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.

Cherry added 2 commits October 4, 2026 20:58
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 <james@jross.me>
Signed-off-by: James Ross <james@jross.me>
@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.42%. Comparing base (7e39b87) to head (f2f0fc7).
⚠️ Report is 45 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66514      +/-   ##
==========================================
- Coverage   90.42%   90.42%   -0.01%     
==========================================
  Files         790      790              
  Lines      275427   275445      +18     
  Branches    52823    52829       +6     
==========================================
+ Hits       249053   249062       +9     
  Misses      16783    16783              
- Partials     9591     9600       +9     
Files with missing lines Coverage Δ
lib/internal/modules/cjs/loader.js 98.01% <100.00%> (+0.02%) ⬆️

... and 26 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.

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