From cfa26190df4da614c5b132af84a9d4f54ccda943 Mon Sep 17 00:00:00 2001 From: Raymond Cohen Date: Thu, 21 Sep 2023 17:36:17 -0400 Subject: [PATCH] cache realpathSync access in package-cache.get --- packages/shared-internals/src/package-cache.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/shared-internals/src/package-cache.ts b/packages/shared-internals/src/package-cache.ts index c679cd929..8669227e6 100644 --- a/packages/shared-internals/src/package-cache.ts +++ b/packages/shared-internals/src/package-cache.ts @@ -4,6 +4,19 @@ import { getOrCreate } from './get-or-create'; import resolvePackagePath from 'resolve-package-path'; import { dirname, sep } from 'path'; +const realpathSyncCache = new Map(); + +function getCachedRealpath(path: string): string { + let cached = realpathSyncCache.get(path); + if (cached) { + return cached; + } + + let root = realpathSync(path); + realpathSyncCache.set(path, root); + return root; +} + export default class PackageCache { constructor(public appRoot: string) {} @@ -31,7 +44,7 @@ export default class PackageCache { private resolutionCache: Map> = new Map(); get(packageRoot: string) { - let root = realpathSync(packageRoot); + let root = getCachedRealpath(packageRoot); let p = getOrCreate(this.rootCache, root, () => { return new Package(root, this, root === this.appRoot); });