Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit f50b474

Browse files
committed
perf: add cached version of isDir
1 parent 4ba2f87 commit f50b474

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ function cachedIsFile (file, cb) {
3737
isFileCache[file].then(contents => cb(null, contents), cb);
3838
}
3939

40+
let isDirCache = {};
41+
function cachedIsDir (dir, cb) {
42+
if (dir in isDirCache === false) {
43+
isDirCache[dir] = statAsync(dir)
44+
.then(
45+
stat => stat.isDirectory(),
46+
err => {
47+
if (err.code === 'ENOENT') return false;
48+
delete isDirCache[dir];
49+
throw err;
50+
});
51+
}
52+
isDirCache[dir].then(contents => cb(null, contents), cb);
53+
}
54+
55+
4056
function getMainFields (options) {
4157
let mainFields;
4258
if (options.mainFields) {
@@ -182,6 +198,7 @@ export default function nodeResolve ( options = {} ) {
182198
},
183199
readFile: cachedReadFile,
184200
isFile: cachedIsFile,
201+
isDir: cachedIsDir,
185202
extensions: extensions
186203
};
187204

0 commit comments

Comments
 (0)