var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ];
var other = params.push( [1, 2]);
var params = [ "hello", true, 7 ];
Array.prototype.push.apply(params, [1, 2]);
var params = [ "hello", true, 7 ];
var other = params.push.apply(params, [1, 2]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
apply proto | |
params.push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 17030786.0 Ops/sec |
spread operator | 68333856.0 Ops/sec |
apply proto | 9461015.0 Ops/sec |
params.push | 67424496.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The benchmark being tested is the performance difference between three approaches to merge two arrays in JavaScript: Array.prototype.concat()
, the spread operator (...
), and Array.prototype.push.apply()
.
What are we comparing?
We're comparing the execution time of each approach when merging a fixed-size array ([ "hello", true, 7 ]
) with an already existing array ([1, 2]
). The three approaches to merge these arrays are:
Array.prototype.concat()
: This method creates a new array by concatenating two or more arrays....
): This syntax allows you to expand an iterable (like an array) into individual elements.Array.prototype.push.apply()
: This method adds multiple elements to the end of an array.Pros and Cons of each approach:
Array.prototype.concat()
:...
):concat()
, avoids creating a new array.Array.prototype.push.apply()
:concat()
or spread operator, may lead to unexpected behavior if not used carefully.Library and special JavaScript features:
In this benchmark, none of the libraries are explicitly mentioned. However, it's worth noting that the spread operator was introduced in ES6 (ECMAScript 2015), which means older browsers and environments might not support it.
No special JavaScript features are explicitly mentioned in this benchmark.
Benchmark preparation code:
The script preparation code is null, indicating that no specific setup or initialization is required for the benchmark. This is likely because the focus is on measuring the performance of these three approaches.
Individual test cases:
Each test case represents a single execution of one of the three approaches. The Test Name
field indicates which approach is being measured:
Array.prototype.concat()
: Merges [ "hello", true, 7 ]
with [1, 2]
....
): Expands [ "hello", true, 7 ]
into individual elements and merges them with [1, 2]
.Array.prototype.push.apply()
: Adds [1, 2]
to the end of [ "hello", true, 7 ]
.params.push()
: This test case is slightly different; it merges two arrays using push()
, which adds elements to the end of an array.Other alternatives:
If you want to explore other approaches for merging arrays in JavaScript, consider the following:
Array.prototype.slice().concat()
: Creates a new array by concatenating slices of the original arrays.Array.prototype.splice()
: Modifies the existing array by inserting or removing elements.Array.prototype.unshift()
: Adds elements to the beginning of an array (not applicable for merging two arrays).reduce()
or other aggregation methods to merge arrays.Keep in mind that each approach has its own trade-offs, and the best choice depends on your specific use case and performance requirements.