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

perf: add cached version of isDir #218

Merged
merged 4 commits into from
May 11, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ function cachedIsFile (file, cb) {
isFileCache[file].then(contents => cb(null, contents), cb);
}

let isDirCache = {};
function cachedIsDir (dir, cb) {
if (dir in isDirCache === false) {
isDirCache[dir] = statAsync(dir)
.then(
stat => stat.isDirectory(),
err => {
if (err.code === 'ENOENT') return false;
delete isDirCache[dir];
throw err;
});
}
isDirCache[dir].then(contents => cb(null, contents), cb);
}


function getMainFields (options) {
let mainFields;
if (options.mainFields) {
Expand Down Expand Up @@ -182,6 +198,7 @@ export default function nodeResolve ( options = {} ) {
},
readFile: cachedReadFile,
isFile: cachedIsFile,
isDir: cachedIsDir,
extensions: extensions
};

Expand Down