Skip to content

Commit 1a21e0f

Browse files
anonrigaduh95
authored andcommitted
esm: improve defaultResolve performance
PR-URL: #53711 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 7debb6c commit 1a21e0f

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

lib/internal/modules/esm/resolve.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
3333
const experimentalNetworkImports =
3434
getOptionValue('--experimental-network-imports');
3535
const inputTypeFlag = getOptionValue('--input-type');
36-
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
36+
const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
3737
const { getCWDURL, setOwnProperty } = require('internal/util');
3838
const { canParse: URLCanParse } = internalBinding('url');
3939
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
@@ -1054,20 +1054,17 @@ function defaultResolve(specifier, context = {}) {
10541054

10551055
let parsedParentURL;
10561056
if (parentURL) {
1057-
try {
1058-
parsedParentURL = new URL(parentURL);
1059-
} catch {
1060-
// Ignore exception
1061-
}
1057+
parsedParentURL = URLParse(parentURL);
10621058
}
10631059

10641060
let parsed, protocol;
1065-
try {
1066-
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
1067-
parsed = new URL(specifier, parsedParentURL);
1068-
} else {
1069-
parsed = new URL(specifier);
1070-
}
1061+
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
1062+
parsed = URLParse(specifier, parsedParentURL);
1063+
} else {
1064+
parsed = URLParse(specifier);
1065+
}
1066+
1067+
if (parsed != null) {
10711068
// Avoid accessing the `protocol` property due to the lazy getters.
10721069
protocol = parsed.protocol;
10731070

@@ -1090,11 +1087,6 @@ function defaultResolve(specifier, context = {}) {
10901087
) {
10911088
return { __proto__: null, url: parsed.href };
10921089
}
1093-
} catch (e) {
1094-
if (e?.code === 'ERR_NETWORK_IMPORT_DISALLOWED') {
1095-
throw e;
1096-
}
1097-
// Ignore exception
10981090
}
10991091

11001092
// There are multiple deep branches that can either throw or return; instead

0 commit comments

Comments
 (0)