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

20% npm install speedup: cache extras by package.json string #70

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ readJson.extraSet = [
]

var typoWarned = {}
var cache = {}

function readJson (file, log_, strict_, cb_) {
var log, strict, cb
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
})
}

Expand Down