Skip to content

module: cache URL conversions for CJS resolve hooks - #68021

Closed
Nedomas wants to merge 1 commit into
nodejs:mainfrom
Nedomas:module-cjs-hooks-url-cache
Closed

Nedomas wants to merge 1 commit into
nodejs:mainfrom
Nedomas:module-cjs-hooks-url-cache

Conversation

@Nedomas

@Nedomas Nedomas commented Dec 4, 2026

Copy link
Copy Markdown

When resolve hooks are registered with module.registerHooks(), every require() that misses the relative resolve cache converts the filename from the default resolver to a URL with pathToFileURL() for the hooks, then converts the URL returned by the hooks back with fileURLToPath(). The same file is usually resolved from many parent directories, so the same conversions repeat: requiring jsdom, @babel/core, eslint/use-at-your-own-risk and webpack resolves 3,198 files through the hooks, of which 1,126 are unique.

This caches both conversions per filename returned by the default resolver. The filename for the URL returned by the hooks is only taken from the cache when that URL is the one the default resolver produced, so hooks that return a different URL are converted as before. Rooted paths without a drive letter on Windows are not cached, as their conversion depends on the current working directory.

Benchmark added in benchmark/module/module-require-hooks.js (1,000 parent directories each requiring the same 10 packages, with and without a pass-through resolve hook), macOS arm64, 30 runs:

                                                                  confidence improvement accuracy (*)   (**)  (***)
module/module-require-hooks.js n=1000 packages=10 hooks='none'                   -0.42 %       ±1.05% ±1.40% ±1.82%
module/module-require-hooks.js n=1000 packages=10 hooks='resolve'        ***     12.05 %       ±1.06% ±1.42% ±1.85%

The pass-through hook went from 13.7% slower than no hooks to 3.0% slower. On the package graph above, the median time to require it with a pass-through resolve hook went from 177.6 ms to 170.5-173.9 ms (166.5 ms without hooks).

When resolve hooks are registered with module.registerHooks(), every
require() that misses the relative resolve cache converts the filename
from the default resolver to a URL for the hooks with pathToFileURL(),
then converts the URL returned by the hooks back with fileURLToPath().
The same file is usually resolved from many parent directories, so
the same conversions repeat.

Cache both conversions per filename returned by the default resolver.
The filename for the URL returned by the hooks is only taken from the
cache when that URL is the one the default resolver produced, so hooks
that return a different URL are converted as before. Rooted paths
without a drive letter on Windows are not cached, as their conversion
depends on the current working directory.

Signed-off-by: Domas Bitvinskas <domas.bitvinskas@me.com>
@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 Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

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.

@Nedomas Nedomas closed this Dec 4, 2026
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