Skip to content

Commit

Permalink
core: faster saveTrace by streaming 500 events at a time (#5387)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Jun 11, 2018
1 parent e02d517 commit e71bad1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ async function prepareAssets(artifacts, audits) {
}

/**
* Generates a JSON representation of traceData line-by-line to avoid OOM due to
* very large traces.
* Generates a JSON representation of traceData line-by-line to avoid OOM due to very large traces.
* COMPAT: As of Node 9, JSON.parse/stringify can handle 256MB+ strings. Once we drop support for
* Node 8, we can 'revert' PR #2593. See https://stackoverflow.com/a/47781288/89484
* @param {LH.Trace} traceData
* @return {IterableIterator<string>}
*/
function* traceJsonGenerator(traceData) {
const EVENTS_PER_ITERATION = 500;
const keys = Object.keys(traceData);

yield '{\n';
Expand All @@ -211,9 +213,19 @@ function* traceJsonGenerator(traceData) {
// Emit first item manually to avoid a trailing comma.
const firstEvent = eventsIterator.next().value;
yield ` ${JSON.stringify(firstEvent)}`;

let eventsRemaining = EVENTS_PER_ITERATION;
let eventsJSON = '';
for (const event of eventsIterator) {
yield `,\n ${JSON.stringify(event)}`;
eventsJSON += `,\n ${JSON.stringify(event)}`;
eventsRemaining--;
if (eventsRemaining === 0) {
yield eventsJSON;
eventsRemaining = EVENTS_PER_ITERATION;
eventsJSON = '';
}
}
yield eventsJSON;
}
yield '\n]';

Expand Down

0 comments on commit e71bad1

Please sign in to comment.