Skip to content

Commit b3b8349

Browse files
authored
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 cbd2c38 commit b3b8349

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 8 additions & 14 deletions
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,21 +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+
}
10711066

1067+
if (parsed != null) {
10721068
// Avoid accessing the `protocol` property due to the lazy getters.
10731069
protocol = parsed.protocol;
10741070
if (protocol === 'data:' ||
@@ -1081,8 +1077,6 @@ function defaultResolve(specifier, context = {}) {
10811077
) {
10821078
return { __proto__: null, url: parsed.href };
10831079
}
1084-
} catch {
1085-
// Ignore exception
10861080
}
10871081

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

0 commit comments

Comments
 (0)