Skip to content

Commit 5d75ec4

Browse files
anonrigMoLow
authored andcommitted
module: reduce the number of URL initializations
PR-URL: #48272 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1cde4a4 commit 5d75ec4

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
@@ -3,6 +3,7 @@
33
// If user passed `-c` or `--check` arguments to Node, check its syntax
44
// instead of actually running the file.
55

6+
const { URL } = require('internal/url');
67
const {
78
prepareMainThreadExecution,
89
markBootstrapComplete,
@@ -55,7 +56,7 @@ async function checkSyntax(source, filename) {
5556
const { defaultResolve } = require('internal/modules/esm/resolve');
5657
const { defaultGetFormat } = require('internal/modules/esm/get_format');
5758
const { url } = await defaultResolve(pathToFileURL(filename).toString());
58-
const format = await defaultGetFormat(url);
59+
const format = await defaultGetFormat(new URL(url));
5960
isModule = format === 'module';
6061
}
6162
if (isModule) {

lib/internal/modules/esm/get_format.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const experimentalNetworkImports =
2020
const experimentalSpecifierResolution =
2121
getOptionValue('--experimental-specifier-resolution');
2222
const { getPackageType, getPackageScopeConfig } = require('internal/modules/esm/resolve');
23-
const { URL, fileURLToPath } = require('internal/url');
23+
const { fileURLToPath } = require('internal/url');
2424
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
2525

2626
const protocolHandlers = {
@@ -99,27 +99,29 @@ function getHttpProtocolModuleFormat(url, context) {
9999
}
100100

101101
/**
102-
* @param {URL | URL['href']} url
102+
* @param {URL} url
103103
* @param {{parentURL: string}} context
104104
* @returns {Promise<string> | string | undefined} only works when enabled
105105
*/
106106
function defaultGetFormatWithoutErrors(url, context) {
107-
const parsed = new URL(url);
108-
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol))
107+
const protocol = url.protocol;
108+
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
109109
return null;
110-
return protocolHandlers[parsed.protocol](parsed, context, true);
110+
}
111+
return protocolHandlers[protocol](url, context, true);
111112
}
112113

113114
/**
114-
* @param {URL | URL['href']} url
115+
* @param {URL} url
115116
* @param {{parentURL: string}} context
116117
* @returns {Promise<string> | string | undefined} only works when enabled
117118
*/
118119
function defaultGetFormat(url, context) {
119-
const parsed = new URL(url);
120-
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ?
121-
protocolHandlers[parsed.protocol](parsed, context, false) :
122-
null;
120+
const protocol = url.protocol;
121+
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
122+
return null;
123+
}
124+
return protocolHandlers[protocol](url, context, false);
123125
}
124126

125127
module.exports = {

lib/internal/modules/esm/load.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ async function defaultLoad(url, context) {
7777
source,
7878
} = context;
7979

80-
throwIfUnsupportedURLScheme(new URL(url), experimentalNetworkImports);
80+
const urlInstance = new URL(url);
8181

82-
if (format == null) {
83-
format = await defaultGetFormat(url, context);
84-
}
82+
throwIfUnsupportedURLScheme(urlInstance, experimentalNetworkImports);
83+
84+
format ??= await defaultGetFormat(urlInstance, context);
8585

8686
validateAssertions(url, format, importAssertions);
8787

0 commit comments

Comments
 (0)