Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache repeated directory lookups for 12% speedup #356

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-laws-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/eslint-plugin': patch
---

Speedup polaris linting rules by about 12% via caching already resolved files.
15 changes: 14 additions & 1 deletion packages/eslint-plugin/lib/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ function getImportDetailsForName(name, context) {

const INDEX_FILE = /^index\./;
const IS_FILE = /(^|\/).*\..*$/;

// Calling `normalizeSource` repeatedly with the same arguments is expensive as
// it traverses the file system upwards in search for a specific directory. By
// caching the normalized value we trade a little bit of additional memory with
// a 12% speedup in total linting times for bigger projects.
const normalizationCache = new Map();

function normalizeSource(source, context) {
if (normalizationCache.has(source)) {
return normalizationCache.get(source);
}

const sourceRoot = pkgDir.sync(source);
const packageRoot = pkgDir.sync(context.getFilename());
const relativeSource =
Expand All @@ -84,7 +95,9 @@ function normalizeSource(source, context) {
? sourceDir
: join(sourceDir, sourceBasename.split('.').slice(0, -1).join('.'));

return sourceWithoutExtension.replace(/^node_modules\//, '');
const normalized = sourceWithoutExtension.replace(/^node_modules\//, '');
normalizationCache.set(source, normalized);
return normalized;
}

function findDefinition(name, context) {
Expand Down