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 = Array.prototype.push.apply([ 1, 2], params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 3585397.2 Ops/sec |
spread operator | 23185216.0 Ops/sec |
Push | 2852304.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition The benchmark is designed to compare three different methods for concatenating arrays in JavaScript:
concat()
method: This method uses the concat()
function to concatenate two or more arrays....
): This operator was introduced in ES6 (ECMAScript 2015) and allows you to expand an array by spreading its elements into another array.push()
method: This method adds one or more elements to the end of an array using the push()
function.Benchmark Preparation Code There is no specific code provided, but we can infer that the benchmark assumes a basic JavaScript environment with arrays and the methods being tested are available.
Individual Test Cases
Array.prototype.concat
: This test case uses the traditional concat()
method to concatenate an array ([ 1, 2 ]
) with another array containing some parameters ("hello"
, true
, and 7
).params
array into a new array called other
. The syntax is [ 1, 2, ...params ]
.Push()
method: This test case uses the push()
method to add elements from an array (params
) to another array ([ 1, 2 ]
). The syntax is Array.prototype.push.apply([ 1, 2], params);
.Library and Purpose None of these methods rely on external libraries.
Special JS Feature or Syntax
The spread operator (...
) is a special feature introduced in ES6. It allows you to expand an array into another array by spreading its elements.
Pros and Cons
concat()
method:...
):Push()
method:concat()
for large arrays since it modifies the existing array.Other Alternatives
Before the spread operator was introduced, developers could use other methods like Array.prototype.slice()
, Array.prototype.splice()
, or even libraries like Lodash (_.concat()
).
In summary, this benchmark compares three popular ways to concatenate arrays in JavaScript: the traditional concat()
method, the concise and efficient spread operator, and the push()
method.