Skip to content

Instantly share code, notes, and snippets.

@smikula
Last active May 4, 2022 21:04
Show Gist options
  • Select an option

  • Save smikula/0e85fd9662f020f6cd45c98ee89ebd8a to your computer and use it in GitHub Desktop.

Select an option

Save smikula/0e85fd9662f020f6cd45c98ee89ebd8a to your computer and use it in GitHub Desktop.
Reading a BIG stats.json file
let JSONStream = require('jsonstream');
let fs = require('fs');
let es = require('event-stream');
function readStatsJson(path) {
return new Promise((resolve, reject) => {
let parsedObject = {};
let stream = fs
.createReadStream(path, { encoding: 'utf8' })
.pipe(JSONStream.parse('$*'))
.pipe(
es.mapSync(data => {
parsedObject[data.key] = data.value;
})
);
stream.on('close', () => {
resolve(parsedObject);
});
stream.on('error', reject);
});
}
@smikula
Copy link
Author

smikula commented Dec 13, 2018

When webpack's stats get really big, it isn't possible to read in the file as a single string. (You'll get RangeError: Invalid string length.) This is a solution to read and parse the file via a stream. Given how big the stats are, you'll probably need to give node more memory (e.g. --max-old-space-size=4096).

Note that you can't write an overlarge stats object with JSONStream, because internally it still tries to use the native JSON.stringify on the object. For that, the only thing I found that works is BFJ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment