Skip to content

Commit

Permalink
use an LRU cache for the getPolyfillMeta function to avoid too many f…
Browse files Browse the repository at this point in the history
…ilesystem operations
  • Loading branch information
Jake Champion authored and JakeChampion committed Feb 27, 2019
1 parent 9f7ea87 commit 4c37562
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const fs = require("graceful-fs");
const denodeify = require("denodeify");
const readFile = denodeify(fs.readFile);
const readdir = denodeify(fs.readdir);
const LRUCache = require('mnemonist/lru-cache');
const polyfillMetaCache = new LRUCache(1000);

const polyfillDirectory = path.join(__dirname, "../polyfills/__dist");
/**
Expand All @@ -13,12 +15,16 @@ const polyfillDirectory = path.join(__dirname, "../polyfills/__dist");
* @returns {Promise<Object|undefined>} A promise which resolves with the metadata or with `undefined` if no metadata exists for the polyfill.
*/
function getPolyfillMeta(featureName) {
return readFile(
let meta = polyfillMetaCache.get(featureName);
if (meta === undefined) {
meta = readFile(
path.join(polyfillDirectory, featureName, "meta.json"),
"UTF-8"
)
.then(JSON.parse)
).then(JSON.parse)
.catch(() => undefined);
polyfillMetaCache.set(featureName, meta);
}
return meta;
}

/**
Expand Down

0 comments on commit 4c37562

Please sign in to comment.