Skip to content
This repository has been 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/index.js
Expand Up @@ -37,6 +37,22 @@ function cachedIsFile (file, cb) {
isFileCache[file].then(contents => cb(null, contents), cb);
}

let isDirCache = {};
keithamus marked this conversation as resolved.
Show resolved Hide resolved
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);
}
keithamus marked this conversation as resolved.
Show resolved Hide resolved


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