Skip to content

Commit

Permalink
Merge pull request #1612 from raycohen/cache-exists-calls-in-package-…
Browse files Browse the repository at this point in the history
…cache

performance: cache existsSync results in PackageCache.ownerOfFile
  • Loading branch information
ef4 authored Sep 26, 2023
2 parents a449708 + 0ce95f3 commit f89a1c7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/shared-internals/src/package-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ function getCachedRealpath(path: string): string {
return root;
}

const existsCache = new Map<string, boolean>();

function getCachedExists(path: string): boolean {
if (existsCache.has(path)) {
const cachedExists = existsCache.get(path);
if (cachedExists !== undefined) {
return cachedExists;
}
}

const exists = existsSync(path);
existsCache.set(path, exists);
return exists;
}

export default class PackageCache {
constructor(public appRoot: string) {}

Expand Down Expand Up @@ -68,7 +83,7 @@ export default class PackageCache {
if (this.rootCache.has(candidate)) {
return this.rootCache.get(candidate);
}
if (existsSync([...usedSegments, 'package.json'].join(sep))) {
if (getCachedExists([...usedSegments, 'package.json'].join(sep))) {
return this.get(candidate);
}
}
Expand Down

0 comments on commit f89a1c7

Please sign in to comment.