Skip to content

Commit 6d6bf3e

Browse files
anonrigtargos
authored andcommittedJun 4, 2023
module: reduce the number of URL initializations
PR-URL: #48272 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3e97826 commit 6d6bf3e

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed
 

‎lib/internal/main/check_syntax.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// instead of actually running the file.
55

66
const { getOptionValue } = require('internal/options');
7+
const { URL } = require('internal/url');
78
const {
89
prepareMainThreadExecution,
910
markBootstrapComplete,
@@ -67,7 +68,7 @@ async function checkSyntax(source, filename) {
6768
const { defaultResolve } = require('internal/modules/esm/resolve');
6869
const { defaultGetFormat } = require('internal/modules/esm/get_format');
6970
const { url } = await defaultResolve(pathToFileURL(filename).toString());
70-
const format = await defaultGetFormat(url);
71+
const format = await defaultGetFormat(new URL(url));
7172
isModule = format === 'module';
7273
}
7374

‎lib/internal/modules/esm/get_format.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const {
1717
const experimentalNetworkImports =
1818
getOptionValue('--experimental-network-imports');
1919
const { getPackageType, getPackageScopeConfig } = require('internal/modules/esm/resolve');
20-
const { URL, fileURLToPath } = require('internal/url');
20+
const { fileURLToPath } = require('internal/url');
2121
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
2222

2323
const protocolHandlers = {
@@ -117,27 +117,29 @@ function getHttpProtocolModuleFormat(url, context) {
117117
}
118118

119119
/**
120-
* @param {URL | URL['href']} url
120+
* @param {URL} url
121121
* @param {{parentURL: string}} context
122122
* @returns {Promise<string> | string | undefined} only works when enabled
123123
*/
124124
function defaultGetFormatWithoutErrors(url, context) {
125-
const parsed = new URL(url);
126-
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol))
125+
const protocol = url.protocol;
126+
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
127127
return null;
128-
return protocolHandlers[parsed.protocol](parsed, context, true);
128+
}
129+
return protocolHandlers[protocol](url, context, true);
129130
}
130131

131132
/**
132-
* @param {URL | URL['href']} url
133+
* @param {URL} url
133134
* @param {{parentURL: string}} context
134135
* @returns {Promise<string> | string | undefined} only works when enabled
135136
*/
136137
function defaultGetFormat(url, context) {
137-
const parsed = new URL(url);
138-
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ?
139-
protocolHandlers[parsed.protocol](parsed, context, false) :
140-
null;
138+
const protocol = url.protocol;
139+
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
140+
return null;
141+
}
142+
return protocolHandlers[protocol](url, context, false);
141143
}
142144

143145
module.exports = {

‎lib/internal/modules/esm/load.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ async function defaultLoad(url, context = kEmptyObject) {
7979
source,
8080
} = context;
8181

82-
throwIfUnsupportedURLScheme(new URL(url), experimentalNetworkImports);
82+
const urlInstance = new URL(url);
8383

84-
if (format == null) {
85-
format = await defaultGetFormat(url, context);
86-
}
84+
throwIfUnsupportedURLScheme(urlInstance, experimentalNetworkImports);
85+
86+
format ??= await defaultGetFormat(urlInstance, context);
8787

8888
validateAssertions(url, format, importAssertions);
8989

0 commit comments

Comments
 (0)