const arrayCount = 500;
const arraySize = 4;
var arrays = [];
for(let i = 0; i < arrayCount; i++) {
arrays.push(Array.from({length: arraySize}, () => Math.floor(Math.random() * 40)));
}
let result = [];
arrays.forEach(curr => {result.push(curr)});
let result = [];
arrays.forEach(curr => {result = result.concat(curr)});
let result = [];
result = result.concat(arrays);
let result = [];
arrays.forEach(curr => {Array.prototype.push.apply(result, curr);});
let result = [];
result = Array.prototype.concat.apply(result, arrays);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
push | |
concat | |
concat spread | |
push apply | |
concat apply |
Test name | Executions per second |
---|---|
push | 53422.2 Ops/sec |
concat | 2723.3 Ops/sec |
concat spread | 58486.4 Ops/sec |
push apply | 8999.6 Ops/sec |
concat apply | 56559.1 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is testing the speed of concatenating multiple arrays in JavaScript. The script preparation code creates an array arrays
with 500 elements, each containing an array of length 4 filled with random integers between 0 and 40. The benchmark then tests four different methods to concatenate these arrays:
push
: spreading the inner array into the outer array using Array.prototype.push
.concat
: concatenating the inner array with the outer array using Array.prototype.concat
.concat spread
(introduced in ES6): concatenating the inner array directly with the outer array using the spread operator (...
).push apply
and concat apply
: applying a function to push or concatenate elements into the result array.Library usage
The benchmark uses no external libraries, apart from built-in JavaScript functions like Array.prototype.push
, Array.prototype.concat
, and Array.from
.
Special JS features
The benchmark does not use any special JavaScript features like Web Workers, async/await, or Promises. It only relies on vanilla JavaScript syntax.
Options comparison
Here's a brief summary of the pros and cons of each approach:
push
: Spreading the inner array into the outer array using Array.prototype.push
. This is the most common way to concatenate arrays in JavaScript.concat
: Concatenating the inner array with the outer array using Array.prototype.concat
.push
.push
, especially for large arrays.concat spread
(ES6): Concatenating the inner array directly with the outer array using the spread operator (...
).concat
.push apply
and concat apply
: Applying a function to push or concatenate elements into the result array.Benchmark results
The benchmark results show that concat spread
(ES6) is the fastest method among all options, followed closely by push
. The slower methods (concat
, push apply
, and concat apply
) are likely due to their additional overhead or predictability requirements.
Keep in mind that these results may not be representative for all use cases, as performance can vary depending on specific scenarios and environments.