Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Apr 12, 2021
1 parent 8ba00ce commit aaac433
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,28 @@ async function prepareAssets(artifacts, audits) {
* @return {IterableIterator<string>}
*/
function* arrayOfObjectsJsonGenerator(arrayOfObjects) {
const ELEMENTS_PER_ITERATION = 500;
const ITEMS_PER_ITERATION = 500;

// Stringify and emit trace events separately to avoid a giant string in memory.
// Stringify and emit items separately to avoid a giant string in memory.
yield '[\n';
if (arrayOfObjects.length > 0) {
const elementsIterator = arrayOfObjects[Symbol.iterator]();
// Emit first element manually to avoid a trailing comma.
const firstElement = elementsIterator.next().value;
yield ` ${JSON.stringify(firstElement)}`;

let elementsRemaining = ELEMENTS_PER_ITERATION;
let elementsJSON = '';
for (const element of elementsIterator) {
elementsJSON += `,\n ${JSON.stringify(element)}`;
elementsRemaining--;
if (elementsRemaining === 0) {
yield elementsJSON;
elementsRemaining = ELEMENTS_PER_ITERATION;
elementsJSON = '';
const itemsIterator = arrayOfObjects[Symbol.iterator]();
// Emit first item manually to avoid a trailing comma.
const firstItem = itemsIterator.next().value;
yield ` ${JSON.stringify(firstItem)}`;

let itemsRemaining = ITEMS_PER_ITERATION;
let itemsJSON = '';
for (const item of itemsIterator) {
itemsJSON += `,\n ${JSON.stringify(item)}`;
itemsRemaining--;
if (itemsRemaining === 0) {
yield itemsJSON;
itemsRemaining = ITEMS_PER_ITERATION;
itemsJSON = '';
}
}
yield elementsJSON;
yield itemsJSON;
}
yield '\n]';
}
Expand Down Expand Up @@ -229,6 +229,8 @@ async function saveTrace(traceData, traceFilename) {
const traceIter = traceJsonGenerator(traceData);
const writeStream = fs.createWriteStream(traceFilename);

// TODO: Can remove Readable.from() in Node 13, promisify(pipeline) in Node 15.
// https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback
return pipeline(stream.Readable.from(traceIter), writeStream);
}

Expand Down

0 comments on commit aaac433

Please sign in to comment.