From 2e3a6232a9cb572251b30f345532b529232dd774 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Fri, 10 May 2019 11:41:40 +0100 Subject: [PATCH] refactor: use Map for cache over Object --- src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 0f2a4ac..cc384a6 100644 --- a/src/index.js +++ b/src/index.js @@ -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 => {