diff --git a/.changeset/violet-laws-provide.md b/.changeset/violet-laws-provide.md new file mode 100644 index 00000000..79c6d023 --- /dev/null +++ b/.changeset/violet-laws-provide.md @@ -0,0 +1,5 @@ +--- +'@shopify/eslint-plugin': patch +--- + +Speedup polaris linting rules by about 12% via caching already resolved files. diff --git a/packages/eslint-plugin/lib/utilities/index.js b/packages/eslint-plugin/lib/utilities/index.js index 16da64d9..6a75ae14 100644 --- a/packages/eslint-plugin/lib/utilities/index.js +++ b/packages/eslint-plugin/lib/utilities/index.js @@ -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 = @@ -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) {