Skip to content

module: add persistent module resolution cache#63927

Open
watilde wants to merge 1 commit into
nodejs:mainfrom
watilde:perf-improvement
Open

module: add persistent module resolution cache#63927
watilde wants to merge 1 commit into
nodejs:mainfrom
watilde:perf-improvement

Conversation

@watilde

@watilde watilde commented Jun 15, 2026

Copy link
Copy Markdown
Member

What

Adds an on-disk module resolution cache, enabled via NODE_MODULE_RESOLVE_CACHE=<dir> or module.enableModuleResolveCache([dir]) (mirroring the module compile cache). It persists Module._pathCache and the ESM resolver results so warm starts of fresh processes skip the node_modules candidate-stat search and package.json reads.

Why

For apps with large dependency trees, module resolution (filesystem stats + package.json reads) dominates startup. The compile cache addresses compilation but not resolution; this fills that gap.

How

  • Binary cache loaded in C++ (no JSON parse on load).
  • Coarse invalidation via a single generation token (Node version/arch, resolution flags, NODE_PATH, lockfile signature) → O(1) validation per process, not per dependency. Changing the lockfile rebuilds the cache.
  • Cache I/O respects the permission model; no-op when not enabled.

Impact

1000-dependency synthetic project, cold child process, wall median:

no cache cache Δ
CommonJS ~80 ms ~51 ms −36%
ESM ~62 ms ~41 ms −34%

Tests / docs

  • test/parallel/test-module-resolve-cache.js (CJS + ESM, lockfile invalidation, API, disabled-by-default).
  • benchmark/module/resolve-cache.js.
  • Docs in doc/api/module.md and doc/api/cli.md.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jun 15, 2026
Cache module resolution results on disk, enabled via
NODE_MODULE_RESOLVE_CACHE=<dir> or module.enableModuleResolveCache().
Warm starts skip the node_modules search and package.json reads,
cutting resolution time by ~36% (CJS) / ~34% (ESM) on a 1000-dep
project. The binary cache is validated coarsely by a generation token
(Node version, flags, lockfile signature) and respects the permission
model.

Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com>
@watilde watilde force-pushed the perf-improvement branch from 29fa3dc to 8711c18 Compare June 15, 2026 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants