Skip to content

Commit 5986130

Browse files
committed
cache fs calls
1 parent a42f476 commit 5986130

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

packages/jest-resolve/src/fileWalkers.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function clearFsCache(): void {
1414
checkedPaths.clear();
1515
checkedRealpathPaths.clear();
1616
packageContents.clear();
17+
packageJsonInDir.clear();
1718
}
1819

1920
enum IPathType {
@@ -88,6 +89,21 @@ export function readPackageCached(path: Config.Path): PkgJson {
8889
return result;
8990
}
9091

92+
const packageJsonInDir = new Map<string, boolean>();
93+
function directoryHasPackageJson(file: Config.Path): boolean {
94+
let cachedValue = packageJsonInDir.get(file);
95+
96+
if (cachedValue != null) {
97+
return cachedValue;
98+
}
99+
100+
cachedValue = fs.existsSync(file);
101+
102+
packageJsonInDir.set(file, cachedValue);
103+
104+
return cachedValue;
105+
}
106+
91107
// adapted from https://github.com/lukeed/escalade/blob/2477005062cdbd8407afc90d3f48f4930354252b/src/sync.js to use cached `fs.stat` calls
92108
export function findClosestPackageJson(
93109
start: Config.Path,
@@ -98,16 +114,17 @@ export function findClosestPackageJson(
98114
}
99115

100116
while (true) {
101-
let tmp = resolve(dir, './package.json');
117+
const pkgJsonFile = resolve(dir, './package.json');
118+
const hasPackageJson = directoryHasPackageJson(pkgJsonFile);
102119

103-
if (isFile(tmp)) {
104-
return tmp;
120+
if (hasPackageJson) {
121+
return pkgJsonFile;
105122
}
106123

107-
tmp = dir;
124+
const prevDir = dir;
108125
dir = dirname(dir);
109126

110-
if (tmp === dir) {
127+
if (prevDir === dir) {
111128
return undefined;
112129
}
113130
}

0 commit comments

Comments
 (0)