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 d23c113
Showing 1 changed file with 14 additions and 1 deletion.
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 d23c113

Please sign in to comment.