Skip to content

module: cache "exports" resolutions in _findPath - #66514

Open
Cherry wants to merge 2 commits into
nodejs:mainfrom
Cherry:perf/cjs-resolve-cache-loaded-modules
Open

Cherry wants to merge 2 commits into
nodejs:mainfrom
Cherry:perf/cjs-resolve-cache-loaded-modules

Conversation

@Cherry

@Cherry Cherry commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

We noticed this while profiling ESLint on our projects. eslint-plugin-lodash-template calls require("espree") on every parse, and espree is already loaded by ESLint from a different directory, so each call was resolved from disk again. In one lint run that was 297 repeat resolutions taking ~70 ms on Windows, all for packages that use "exports".

Module._findPath() caches results in Module._pathCache, but packages resolved through "exports" return before that write. #32351 added that early return; before it, "exports" targets fell through to the cached path - I'm not sure if this was intentional or not, but can't see any clear reason why.

This caches those results separately, only for bare specifiers and the default conditions, since the _pathCache key doesn't include conditions. Replacing Module._pathCache clears it, and a VFS unmount purges it like the other module caches.

require() of a loaded package from another directory before after
with "exports" 8,400 ops/s ~420,000 ops/s
without "exports" unchanged unchanged

Windows x64 release build, benchmark/module/module-require-exports.js.

One behavior difference: if node_modules changes on disk while the process runs, for example a closer copy of an "exports" package is installed, or its target file is deleted, a repeat require() with the same lookup paths returns the earlier resolution instead of checking the disk again. Packages without "exports" already behave this way through _pathCache, as "exports" packages did before #32351.

Each entry holds the key and resolved path strings, resulting in about 75 KB for the ~135 entries in that lint run.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.58%. Comparing base (91c4436) to head (3a837a2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66514      +/-   ##
==========================================
- Coverage   92.93%   90.58%   -2.36%     
==========================================
  Files         428      797     +369     
  Lines      195236   278322   +83086     
  Branches    30176    53520   +23344     
==========================================
+ Hits       181442   252114   +70672     
- Misses      13467    16577    +3110     
- Partials      327     9631    +9304     
Files with missing lines Coverage Δ
lib/internal/modules/cjs/loader.js 98.01% <100.00%> (+19.21%) ⬆️

... and 500 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Module._findPath() returns packages resolved through package.json
"exports" before writing Module._pathCache, so requiring an already
loaded package from another directory resolves it from disk again.

Cache those results for bare specifiers and the default conditions,
which the _pathCache key leaves out. Replacing Module._pathCache still
forces a fresh resolution.

Signed-off-by: James Ross <[email protected]>
@Cherry
Cherry force-pushed the perf/cjs-resolve-cache-loaded-modules branch from f2f0fc7 to 3a837a2 Compare October 24, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:mixed-signals module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants