From 3f6121500a4d54007cdd62b0debf3472c0691b2f Mon Sep 17 00:00:00 2001 From: Mike Sherov Date: Mon, 19 Jun 2017 23:55:04 -0400 Subject: [PATCH] cache extras by package.json string --- read-json.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/read-json.js b/read-json.js index 26986b2..d65c33a 100644 --- a/read-json.js +++ b/read-json.js @@ -26,6 +26,7 @@ readJson.extraSet = [ ] var typoWarned = {} +var cache = {} function readJson (file, log_, strict_, cb_) { var log, strict, cb @@ -72,14 +73,27 @@ function parseJson (file, er, d, log, strict, cb) { } if (er) return cb(er) + if (cache[d]) return cb(null, cache[d]) + + var data + try { - d = safeJSON.parse(stripBOM(d)) + data = safeJSON.parse(stripBOM(d)) } catch (er) { - d = parseIndex(d) - if (!d) return cb(parseError(er, file)) + data = parseIndex(d) + if (!data) return cb(parseError(er, file)) } - extras(file, d, log, strict, cb) + extrasCached(file, d, data, log, strict, cb) +} + +function extrasCached (file, d, data, log, strict, cb) { + extras(file, data, log, strict, (err, data) => { + if (!err) { + cache[d] = data + } + cb(err, data) + }) } function indexjs (file, er, log, strict, cb) { @@ -89,10 +103,12 @@ function indexjs (file, er, log, strict, cb) { fs.readFile(index, 'utf8', function (er2, d) { if (er2) return cb(er) - d = parseIndex(d) - if (!d) return cb(er) + if (cache[d]) return cb(null, cache[d]) + + var data = parseIndex(d) + if (!data) return cb(er) - extras(file, d, log, strict, cb) + extrasCached(file, d, data, log, strict, cb) }) }