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

Commit

Permalink
refactor: use Map for cache over Object
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed May 10, 2019
1 parent 774c954 commit 2e3a623
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ];
const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents)));
const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents)));
const cache = fn => {
let cache = Object.create(null);
const wrapped = (s, cb = false) => {
if (s in cache === false) {
const cache = new Map();
const wrapped = (s, cb) => {
if (cache.has(s) === false) {
cache[s] = fn(s).catch(err => {
delete cache[s];
cache.delete(s);
throw err;
});
}
return cb ? cache[s].then(v => cb(null, v), cb) : cache[s];
return cache.get(s).then(v => cb(null, v), cb);
};
wrapped.clear = () => { cache = {}; };
wrapped.clear = () => cache.clear();
return wrapped;
};
const ignoreENOENT = err => {
Expand Down

0 comments on commit 2e3a623

Please sign in to comment.