Skip to content

perf: Improve performance by caching the isUsingTranspiler function#680

Merged
amilajack merged 2 commits intoamilajack:mainfrom
igeligel:memoize-isUsingTranspiler
Feb 23, 2026
Merged

perf: Improve performance by caching the isUsingTranspiler function#680
amilajack merged 2 commits intoamilajack:mainfrom
igeligel:memoize-isUsingTranspiler

Conversation

@igeligel
Copy link
Copy Markdown
Contributor

What this PR is doing?

We are memoizing the isUsingTranspiler. File reads are not great in large mono repositories. Where I work this is a bottleneck. Specifically for directories with a large number of files this can blow up the time a lot.

This PR also fixes a minor bug. The function took the context and computed the directory like const dir = context.filename ?? context.getFilename();.

The problem is that this is passing the file name instead of the expected directory to findUp. This is also fixed in this PR. I have added tests as well.

Benchmarks

Gist: https://gist.github.com/igeligel/af1961949a79d91e4855fc017b1d75ef

Repo Before mean (s) After mean (s) Change
electron-react-boilerplate 0.0316 0.0238 ~25% faster
handlebars.js 0.462 0.319 ~31% faster
jquery 1.089 0.841 ~23% faster
pixi.js 2.284 1.451 ~36% faster
bootstrap 0.609 0.558 ~8% faster
preact 0.00689 0.00550 ~20% faster
aframe 8.070 7.562 ~6% faster
create-react-app 2.685 2.658 ~1% faster

Cache the isUsingTranspiler per directory
@igeligel igeligel changed the title [Performance] Improve performance by caching the isUsingTranspiler function perf: Improve performance by caching the isUsingTranspiler function Feb 15, 2026
@amilajack
Copy link
Copy Markdown
Owner

amilajack commented Feb 18, 2026

thanks for this, happy to merge after fixing merge conflicts

@igeligel
Copy link
Copy Markdown
Contributor Author

Updated @amilajack

@amilajack amilajack merged commit 9c21fdd into amilajack:main Feb 23, 2026
2 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves performance by memoizing the isUsingTranspiler function and fixing a bug where the function was incorrectly passing a filename instead of a directory to findUp. The changes include adding lodash.memoize to cache file system lookups by directory, and an additional optimization that caches getUnsupportedTargets results per rule within each context. Benchmarks show 6-36% performance improvements across various repositories.

Changes:

  • Memoized isUsingTranspiler function to cache babel config detection by directory
  • Fixed bug where filename was passed to findUp instead of directory
  • Added per-context cache for getUnsupportedTargets to avoid redundant computations

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/rules/compat.ts Refactored isUsingTranspiler to use memoization with file path parameter instead of context, fixed directory extraction bug, added unsupportedTargetsByRule cache for additional optimization
test/compat-isUsingTranspiler.spec.ts Added comprehensive integration tests covering babel config detection, memoization behavior, file path handling, and package.json babel property detection

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/rules/compat.ts
}
return false;
},
(filePath: string) => path.resolve(path.dirname(filePath))
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memoization resolver should resolve the file path to an absolute path before extracting the directory. Currently, if a relative file path is passed, path.dirname(filePath) returns a relative directory, and path.resolve resolves it relative to process.cwd(). This could cause cache misses if the same directory is referenced with different relative path formats. Change to path.dirname(path.resolve(filePath)) to ensure consistent cache keys.

Copilot uses AI. Check for mistakes.
github-actions Bot pushed a commit that referenced this pull request Feb 25, 2026
## [6.2.1](v6.2.0...v6.2.1) (2026-02-25)

### Bug Fixes

* case-insensitive match for lowercase browser globals ([#649](#649)) ([#681](#681)) ([367fc02](367fc02))
* format test files with prettier ([4f47dd4](4f47dd4))

### Performance Improvements

* compat ([#680](#680)) ([9c21fdd](9c21fdd))
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 6.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants