Skip to content

Commit

Permalink
Cache repeated directory lookups for 12% speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Jan 9, 2023
1 parent 55a7ce1 commit 0b52b9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit 0b52b9e

Please sign in to comment.