var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2, params ]
var params = [ "hello", true, 7 ];
var other = $.merge([1, 2], params);
var params = [ "hello", true, 7 ];
params.push(1);
params.push(2);
var other = params;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
jQuery merge | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 5174930.5 Ops/sec |
spread operator | 21438100.0 Ops/sec |
jQuery merge | 0.0 Ops/sec |
Push | 28971984.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of three different approaches for creating arrays: Array.prototype.concat
, the spread operator (...
), and the push()
method.
Test Cases
There are four test cases:
concat()
method to concatenate an array with another array....
) to create a new array by spreading elements from an existing array.$merge
function to concatenate arrays.push()
method to add elements to an array and then creates a new array by assigning it to another variable.Comparison
The comparison is between the performance of these four approaches:
Array.prototype.concat
...
)Options Compared
The options compared are:
concat()
, spread operator)$merge
function)Pros and Cons
Here are some pros and cons of each approach:
Array.prototype.concat
Spread Operator (...
)
jQuery merge
Push
push()
operations, which can lead to frequent reallocations of the underlying array.Library Usage
In this benchmark, jQuery is used in the "jQuery merge" test case. The $merge
function concatenates two arrays and returns a new array containing all elements from both input arrays.
Special JS Feature/Syntax
None of the test cases utilize any special JavaScript features or syntax other than modern ES6+ spread operator (...
).