perf(javascript): cache ambient resolver and fix O(n^2) semantic hot paths (#13716)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 18:50:34 -05:00
1 parent aab47683f5
commit 0f987ff71e
6 files changed
+116 -67

No files matched your search

@@ -1703,8 +1703,11 @@ const resolveFileCandidates = (
return null;
};
const ASAR_SEGMENT_PATTERN = /\.asar\//iu;
/** Classify the extension as inventory does; retain exact archive occurrence bytes. */
const runtimeArchiveRoot = (path: string): string | undefined => {
if (!ASAR_SEGMENT_PATTERN.test(path)) return undefined;
const components = path.split("/");
const archive = components.findLastIndex(
(component, index) =>
@@ -2072,16 +2075,39 @@ const selectExportTarget = (
* built-in resolver that owns installed-package imports and requires. The
* supported Node runtimes also activate "module-sync" for both loaders.
*/
const EXPORT_CONDITIONS_DEFAULT: ReadonlySet<string> = new Set([
"node",
"node-addons",
"module-sync",
"default",
]);
const EXPORT_CONDITIONS_BY_KIND = new Map<string, ReadonlySet<string>>([
[
"import",
new Set(["node", "node-addons", "module-sync", "import", "default"]),
],
[
"require",
new Set(["node", "node-addons", "module-sync", "require", "default"]),
],
[
"compiler-dependent",
new Set([
"node",
"node-addons",
"module-sync",
"compiler-dependent",
"default",
]),
],
]);
const packageExportConditions = (
moduleKind: ResolveArtifactPathInput["moduleKind"],
): ReadonlySet<string> =>
new Set([
"node",
"node-addons",
"module-sync",
...(moduleKind === undefined ? [] : [moduleKind]),
"default",
]);
moduleKind === undefined
? EXPORT_CONDITIONS_DEFAULT
: (EXPORT_CONDITIONS_BY_KIND.get(moduleKind) ?? EXPORT_CONDITIONS_DEFAULT);
const notFoundCandidate = (): Extract<
CandidateResolution,