window.top.tests = {control:[], concat:[], spread:[]};
window.test = (new Array(10)).fill(null);
window.cutoff = 5000;
for (let element of window.test) window.top.tests.control.push(element);
if (window.top.tests.control.length > window.cutoff) { window.top.tests.control = []; console.log('reset control'); }
window.top.tests.concat = [window.top.tests.concat,window.test];
if (window.top.tests.concat.length > window.cutoff) { window.top.tests.concat = []; console.log('reset concat'); }
window.top.tests.spread.push(window.test);
if (window.top.tests.spread.length > window.cutoff) { window.top.tests.spread = []; console.log('reset spread'); }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Control (for push) | |
Concat | |
Spread |
Test name | Executions per second |
---|---|
Control (for push) | 116852.1 Ops/sec |
Concat | 27090.1 Ops/sec |
Spread | 443228.4 Ops/sec |
The benchmark defined in the JSON snippet titled "Spread vs push(...) for large arrays" focuses on comparing different methods for appending elements to large arrays in JavaScript. Specifically, it evaluates three approaches: using a traditional for
loop with the push
method, using the concat
method, and utilizing the spread operator. Here's a breakdown of each approach, including their pros and cons, and other considerations.
Control (for push)
for (let element of window.test) window.top.tests.control.push(element);
if (window.top.tests.control.length > window.cutoff) { window.top.tests.control = []; console.log('reset control'); }
window.test
array using a for...of
loop and adds each element to the control
array using the push()
method.push()
.Concat
window.top.tests.concat = [...window.top.tests.concat, window.test];
if (window.top.tests.concat.length > window.cutoff) { window.top.tests.concat = []; console.log('reset concat'); }
...
) to concatenate the existing concat
array with window.test
to create a new array.push()
.Spread
window.top.tests.spread.push(...window.test);
if (window.top.tests.spread.length > window.cutoff) { window.top.tests.spread = []; console.log('reset spread'); }
push()
function combined with the spread operator to append all elements of window.test
to the spread
array.push()
(in-place modification) with the conciseness of the spread operator.push()
in specific circumstances, although it's generally more performant than using concat()
.From the benchmark results, we see the following executions per second for each method:
The results clearly demonstrate that the use of the spread operator with push()
offers significantly better performance compared to traditional for
loops or using concat()
. This is particularly beneficial in contexts involving large arrays where performance is a critical concern.
Other alternatives for appending elements to an array in JavaScript could include:
In conclusion, this benchmark highlights different methods of appending elements to arrays in JavaScript, revealing the performance benefits of using the spread operator in conjunction with the push()
method. Understanding these various techniques can guide engineers in selecting the most efficient and suitable approach for their specific use cases.