diff --git a/packages/shared-internals/src/package-cache.ts b/packages/shared-internals/src/package-cache.ts index 8669227e6..870727dbf 100644 --- a/packages/shared-internals/src/package-cache.ts +++ b/packages/shared-internals/src/package-cache.ts @@ -17,6 +17,21 @@ function getCachedRealpath(path: string): string { return root; } +const existsCache = new Map(); + +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) {} @@ -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); } }