Skip to content

Commit

Permalink
esm: improve defaultResolve performance
Browse files Browse the repository at this point in the history
PR-URL: #53711
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
anonrig authored and aduh95 committed Jul 16, 2024
1 parent 7debb6c commit 1a21e0f
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 1a21e0f

Please sign in to comment.