perf: ImportDelcaration Dirname#1891
Draft
mgfalzon wants to merge 4 commits into
Draft
Conversation
Two targeted improvements to cut per-file cost in the TaggedTemplateExpression|CallExpression visitor and the module resolution path: 1. babel-plugin.ts — hoist isRelative and fileDir out of the importSources.some() callback in ImportDeclaration. dirname() parses the full absolute file path on every iteration; computing it once per ImportDeclaration call removes O(importSources) redundant string allocations per import. 2. resolve-binding.ts — wrap resolveRequest() in cache.load() so that module resolution results are cached for the lifetime of a build (when cache: true). resolve.sync and custom resolvers both walk node_modules and read package.json files synchronously; caching the result eliminates repeated I/O for the same (filename, request) pair. Benchmark (module-traversal-cache, 75 samples): initial run: 137 -> 151 ops/sec (+10%) cache: 181 -> 201 ops/sec (+11%) no-cache: 156 -> 167 ops/sec (+7%)
|
✅ Deploy Preview for compiled-css-in-js canceled.
|
added 3 commits
May 8, 2026 18:17
…tion fileDir is now null when state.filename is absent, and isRelative encodes the null check, eliminating the redundant state.filename guard inside the .some() callback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
One targeted improvement to cut per-file cost in the
ImportDeclarationvisitor.babel-plugin.ts— computerelativeFileDironce upfront instead of inside.some()In the
ImportDeclarationvisitor,dirname(state.filename)and theuserLandModule[0] === '.'check were both computed inside the.some()callback — once per entry inimportSourcesper import declaration.dirname()parses the full absolute file path and allocates a new string on every call. These are now combined into a singlerelativeFileDirvariable computed once before the loop:nullif the import is not relative or there is no filename (skipping resolution entirely), or the resolved directory otherwise.Benchmark
Using the existing
module-traversal-cacheperf test (75 samples each):initial runcacheno-cacheTesting
module-traversal.test.ts(49 tests) pass with no changes.yarn benchmark packages/babel-plugin/src/__perf__/module-traversal-cache.test.ts.