Skip to content

esm: add fast path for cached dynamic import() - #68023

Open
IlyasShabi wants to merge 1 commit into
nodejs:mainfrom
IlyasShabi:ishabi/cached-dynamic-import
Open

IlyasShabi wants to merge 1 commit into
nodejs:mainfrom
IlyasShabi:ishabi/cached-dynamic-import

Conversation

@IlyasShabi

Copy link
Copy Markdown
Member

This PR speeds up repeated dynamic import() calls by reusing the existing cache instead of going through ImportRequest and other functions on every hit.

Changes

  • Cache moduleJob.namespace after evaluation completes.
  • In ModuleLoader.prototype.import() return job.namespace directly when no registerHooks resolve hooks or DC subscribers are active.
  • Skip ObjectKeys() in ResolveCache.prototype.serializeKey() for kEmptyObject.
  • Cache normalizeReferrerURL() results for script referrers.
  • Pass undefined instead of allocating and freezing an empty attributes object in src/module_wrap.cc when no import attributes are present.

Benchmark

using benchmark/compare.js --runs 30 --filter esm-loader-import.js esm:

                                                                      confidence improvement
esm/esm-loader-import.js specifier='./relative-existing.js' n=1000           ***    166.88 %
esm/esm-loader-import.js specifier='./relative-nonexistent.js' n=1000                -3.64 %
esm/esm-loader-import.js specifier='data:text/javascript,{i}' n=1000                 15.09 %
esm/esm-loader-import.js specifier='node:os' n=1000                          ***    189.67 %
esm/esm-loader-import.js specifier='node:prefixed-nonexistent' n=1000                19.01 %

Signed-off-by: Ilyas Shabi <ilyasshabi94@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Dec 4, 2026
@IlyasShabi
IlyasShabi marked this pull request as ready for review December 4, 2026 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants