From b8dd273ae3b04d34dde7228f3e97db43051f2a9e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 6 Jul 2024 06:22:08 -0400 Subject: [PATCH] esm: improve `defaultResolve` performance PR-URL: https://github.com/nodejs/node/pull/53711 Reviewed-By: Geoffrey Booth Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/internal/modules/esm/resolve.js | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 5c0d7ccb091504..66126d9702c5b0 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -33,7 +33,7 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const inputTypeFlag = getOptionValue('--input-type'); -const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url'); +const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url'); const { getCWDURL, setOwnProperty } = require('internal/util'); const { canParse: URLCanParse } = internalBinding('url'); const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs'); @@ -1054,20 +1054,17 @@ function defaultResolve(specifier, context = {}) { let parsedParentURL; if (parentURL) { - try { - parsedParentURL = new URL(parentURL); - } catch { - // Ignore exception - } + parsedParentURL = URLParse(parentURL); } let parsed, protocol; - try { - if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { - parsed = new URL(specifier, parsedParentURL); - } else { - parsed = new URL(specifier); - } + if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { + parsed = URLParse(specifier, parsedParentURL); + } else { + parsed = URLParse(specifier); + } + + if (parsed != null) { // Avoid accessing the `protocol` property due to the lazy getters. protocol = parsed.protocol; @@ -1090,11 +1087,6 @@ function defaultResolve(specifier, context = {}) { ) { return { __proto__: null, url: parsed.href }; } - } catch (e) { - if (e?.code === 'ERR_NETWORK_IMPORT_DISALLOWED') { - throw e; - } - // Ignore exception } // There are multiple deep branches that can either throw or return; instead