let arr1 = new Array(5000).fill(1234567890);
const arr2 = new Array(5000).fill(1234567890);
arr1 = arr1.concat(arr2);
let arr1 = new Array(5000).fill(1234567890);
const arr2 = new Array(5000).fill(1234567890);
Array.prototype.push.apply(arr1,arr2);
let arr1 = new Array(5000).fill(1234567890);
const arr2 = new Array(5000).fill(1234567890);
arr1.push(arr2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
concat | |
prototype push | |
push spread |
Test name | Executions per second |
---|---|
concat | 29339.3 Ops/sec |
prototype push | 13982.5 Ops/sec |
push spread | 4339.3 Ops/sec |
Benchmark Overview
The provided benchmark compares the performance of three different methods for concatenating two arrays in JavaScript: concat
, prototype push
, and push spread
.
What is being tested?
In this benchmark, two arrays with 5000 elements each are created and filled with a large number (1234567890). The benchmark then measures how long it takes to concatenate the two arrays using each of the three methods:
concat
: This method uses the built-in concat
function to merge the two arrays.prototype push
: This method uses the push.apply
method, which applies a function to an array, in this case, the push
method, to add elements to the end of the array.push spread
: This method uses the spread operator (...
) to concatenate the two arrays.Options compared
The benchmark compares the performance of each of these three methods on the same input data (two large arrays).
Pros and Cons of each approach:
concat
:prototype push
:concat
.push spread
:Library/Function used
The benchmark uses the following libraries/functions:
Array.prototype.concat
: The built-in JavaScript function for concatenating arrays.Array.prototype.push.apply
: The built-in JavaScript method for applying a function to an array, which includes pushing elements onto it....
): A syntax feature introduced in ECMAScript 2015 that allows concisely expanding arrays or other iterable objects.Special JS feature/syntax
The benchmark uses the spread operator (...
) to concatenate arrays, which is a modern JavaScript syntax feature introduced in ECMAScript 2015. This feature was added to provide a concise way to merge two arrays into one.
Other alternatives
In addition to the three methods mentioned above, there are other ways to concatenate arrays in JavaScript:
Array.prototype.reduce()
: Using the reduce()
method can also be used to concatenate arrays.for
or while
) to iterate over both arrays and add elements to a new array.However, these alternatives are generally less efficient than using built-in methods like concat
, push.apply
, or the spread operator (...
).