var arr1 = [4, 2, 1, 22, 27, 33, 88];
var arr2 = [4, 2, 1, 55, 53, 22, 44];
arr1 = arr1.concat(arr2);
arr1.push(arr2)
arr1 = [arr1, arr2]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Concat | |
Push | |
Spread syntax |
Test name | Executions per second |
---|---|
Concat | 1563.2 Ops/sec |
Push | 4413375.5 Ops/sec |
Spread syntax | 2.6 Ops/sec |
Overview of the Benchmark
The provided benchmark measures the performance of three different methods for adding an element to an array in JavaScript: push
, concat
, and spread syntax (...
). The test case uses two arrays, arr1
and arr2
, with a total of 7 elements.
Options Compared
push()
method to add an element to the end of the array.concat()
method to concatenate two arrays and return a new array....
) to create a new array by spreading the elements of the original array and adding the new element.Pros and Cons
push
due to the creation of a new array, which can be costly in terms of memory allocation and garbage collection.Library Usage
In this benchmark, no external libraries are used.
Special JS Features/Syntax
...
): This syntax was introduced in ECMAScript 2015 (ES6). It allows for creating new arrays by spreading the elements of an existing array.push()
and concat()
methods are built-in JavaScript methods that do not require any additional libraries or syntax.Other Alternatives
push
could include using splice()
with a negative index, but this is generally less efficient than push
.+
operator (which creates a new array by concatenating elements) or the Array.prototype.reduce()
method.Array.from()
and pushing elements onto an existing array.Benchmark Result
The latest benchmark result shows that:
Keep in mind that these results are specific to the Chrome browser and may vary depending on other factors such as the size of the arrays, device performance, and JavaScript engine optimizations.