Skip to content

Commit

Permalink
performance: cache existsSync results in PackageCache.ownerOfFile
Browse files Browse the repository at this point in the history
  • Loading branch information
raycohen committed Sep 26, 2023
1 parent ace1564 commit c8a5518
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 @@ -4,6 +4,21 @@ import { getOrCreate } from './get-or-create';
import resolvePackagePath from 'resolve-package-path';
import { dirname, sep } from 'path';

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 @@ -55,7 +70,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 c8a5518

Please sign in to comment.