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 Jul 6, 2024
1 parent cbd2c38 commit b3b8349
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 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,21 +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;
if (protocol === 'data:' ||
Expand All @@ -1081,8 +1077,6 @@ function defaultResolve(specifier, context = {}) {
) {
return { __proto__: null, url: parsed.href };
}
} catch {
// Ignore exception
}

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

0 comments on commit b3b8349

Please sign in to comment.