spread vs for loops vs concat

Benchmark created on


Setup

const t = {fields:[]};
const endCounts = Array(100);
const startCounts = Array(100);
const idDeltas = Array(100);
const idRangeOffsets = Array(100);
const glyphIds = Array(100);

Teardown

t.fields = [];

Test runner

Ready to run.

Testing in
TestOps/sec
for() with push()
	for (let i = 0; i < endCounts.length; i++) {
        t.fields.push(endCounts[i]);
    }
    t.fields.push({name: 'reservedPad', type: 'USHORT', value: 0});
    for (let i = 0; i < startCounts.length; i++) {
        t.fields.push(startCounts[i]);
    }
    for (let i = 0; i < idDeltas.length; i++) {
        t.fields.push(idDeltas[i]);
    }
    for (let i = 0; i < idRangeOffsets.length; i++) {
        t.fields.push(idRangeOffsets[i]);
    }
    for (let i = 0; i < glyphIds.length; i++) {
        t.fields.push(glyphIds[i]);
    }
ready
single push() with spread operators
	t.fields.push(
        ...endCounts,
        {name: 'reservedPad', type: 'USHORT', value: 0},
        ...startCounts,
        ...idDeltas,
        ...idRangeOffsets,
        ...glyphIds
    );
ready
using concat()
t.fields = t.fields.concat(
    endCounts,
    [{name: 'reservedPad', type: 'USHORT', value: 0}],
    startCounts,
    idDeltas,
    idRangeOffsets,
    glyphIds
);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.