window.top.tests = {control:[], concat:[], spread:[], flat: []};
window.test = (new Array(10)).fill(null);
window.arrays = (new Array(10)).fill(window.test);
window.cutoff = 5000;
window.top.tests.control = window.arrays.reduce((acc, arr) => {
acc.push(arr);
return acc;
}, []);
if (window.top.tests.control.length > window.cutoff) { window.top.tests.control = []; console.log('reset control'); }
window.top.tests.concat = window.arrays.reduce((acc, arr) => acc.concat(arr), []);
if (window.top.tests.concat.length > window.cutoff) { window.top.tests.concat = []; console.log('reset concat'); }
window.top.tests.spread = window.arrays.reduce((acc, arr) => [acc, arr], []);
if (window.top.tests.spread.length > window.cutoff) { window.top.tests.spread = []; console.log('reset spread'); }
window.top.tests.flat = window.arrays.flat();
if (window.top.tests.flat.length > window.cutoff) { window.top.tests.flat = []; console.log('reset flat'); }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Control (for push) | |
Concat | |
Spread | |
Flat |
Test name | Executions per second |
---|---|
Control (for push) | 325906.3 Ops/sec |
Concat | 192537.9 Ops/sec |
Spread | 225688.7 Ops/sec |
Flat | 197115.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a platform for comparing the performance of different JavaScript approaches to manipulate arrays. The provided benchmark defines four test cases: Control (for push)
, Concat
, Spread
, and Flat
. These tests measure how fast each approach can append elements to an array.
Test Cases and Approaches
push()
method, which appends one or more elements to the end of an array.window.top.tests.control = window.arrays.reduce((acc, arr) => {\r\n acc.push(...arr);\r\n return acc;\r\n}, []);
concat()
method, which returns a new array that is the result of concatenating an array with another array or value.window.top.tests.concat = window.arrays.reduce((acc, arr) => acc.concat(arr), []);
...
) to expand an array into its individual elements and then reassigning them to a new array.window.top.tests.spread = window.arrays.reduce((acc, arr) => [...acc, ...arr], []);
flat()
method, which returns an array with all sub-array elements concatenated into it recursively up to a specified depth.window.top.tests.flat = window.arrays.flat();
Pros and Cons
Each approach has its strengths and weaknesses:
push()
.push()
due to the overhead of expanding the array using the spread operator.flat()
.Library and Special JS Features
The benchmark uses the following libraries:
reduce()
method is used, which is a built-in JavaScript array method that applies a function to each element in an array.No special JS features are mentioned in the benchmark definition.