From 6aabf1a5ccf1849a91ae0e4b2ce1d0fb4e651ceb Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 31 May 2023 16:41:03 -0400 Subject: [PATCH 1/2] module: reduce the number of URL initializations --- lib/internal/main/check_syntax.js | 3 ++- lib/internal/modules/esm/get_format.js | 20 ++++++++++---------- lib/internal/modules/esm/load.js | 8 ++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index 1b32a4d569f494..9355f7b7e4d9be 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -4,6 +4,7 @@ // instead of actually running the file. const { getOptionValue } = require('internal/options'); +const { URL } = require('internal/url'); const { prepareMainThreadExecution, markBootstrapComplete, @@ -67,7 +68,7 @@ async function checkSyntax(source, filename) { const { defaultResolve } = require('internal/modules/esm/resolve'); const { defaultGetFormat } = require('internal/modules/esm/get_format'); const { url } = await defaultResolve(pathToFileURL(filename).toString()); - const format = await defaultGetFormat(url); + const format = await defaultGetFormat(new URL(url)); isModule = format === 'module'; } diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index d8cfb6df710540..8e8671094ab090 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -17,7 +17,7 @@ const { const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const { getPackageType, getPackageScopeConfig } = require('internal/modules/esm/resolve'); -const { URL, fileURLToPath } = require('internal/url'); +const { fileURLToPath } = require('internal/url'); const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; const protocolHandlers = { @@ -117,27 +117,27 @@ function getHttpProtocolModuleFormat(url, context) { } /** - * @param {URL | URL['href']} url + * @param {URL} url * @param {{parentURL: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormatWithoutErrors(url, context) { - const parsed = new URL(url); - if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol)) + const protocol = url.protocol; + if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) return null; - return protocolHandlers[parsed.protocol](parsed, context, true); + return protocolHandlers[protocol](url, context, true); } /** - * @param {URL | URL['href']} url + * @param {URL} url * @param {{parentURL: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormat(url, context) { - const parsed = new URL(url); - return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ? - protocolHandlers[parsed.protocol](parsed, context, false) : - null; + const protocol = url.protocol; + if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) + return null; + return protocolHandlers[protocol](url, context, false); } module.exports = { diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 9ab6f18f3fdda9..c929fcc649c9f1 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -79,11 +79,11 @@ async function defaultLoad(url, context = kEmptyObject) { source, } = context; - throwIfUnsupportedURLScheme(new URL(url), experimentalNetworkImports); + const urlInstance = new URL(url); - if (format == null) { - format = await defaultGetFormat(url, context); - } + throwIfUnsupportedURLScheme(urlInstance, experimentalNetworkImports); + + format ??= await defaultGetFormat(urlInstance, context); validateAssertions(url, format, importAssertions); From 88bde4c21cc2fd94b182d6abc9493d034a980ad1 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 31 May 2023 17:30:11 -0400 Subject: [PATCH 2/2] fixup! module: reduce the number of URL initializations --- lib/internal/modules/esm/get_format.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 8e8671094ab090..4ac9c011d153f4 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -123,8 +123,9 @@ function getHttpProtocolModuleFormat(url, context) { */ function defaultGetFormatWithoutErrors(url, context) { const protocol = url.protocol; - if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) + if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) { return null; + } return protocolHandlers[protocol](url, context, true); } @@ -135,8 +136,9 @@ function defaultGetFormatWithoutErrors(url, context) { */ function defaultGetFormat(url, context) { const protocol = url.protocol; - if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) + if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) { return null; + } return protocolHandlers[protocol](url, context, false); }