var params = [ "hello", true, 7 ];
var other = [ 1, 2,3,4,5,6,7,8,9,10,11,23,23,23,23,23,23,23,23,12,12,22,234,232,23,23,232,23,2321,131,21 ]
var concat = other.concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2,3,4,5,6,7,8,9,10,11,23,23,23,23,23,23,23,23,12,12,22,234,232,23,23,232,23,2321,131,21 ];
var some = [other, params]
var params = [ "hello", true, 7 ];
var other = [ 1, 2,3,4,5,6,7,8,9,10,11,23,23,23,23,23,23,23,23,12,12,22,234,232,23,23,232,23,2321,131,21 ];
other.push(params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 2002007.2 Ops/sec |
spread operator | 1551940.1 Ops/sec |
Push | 4284410.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Overview
The provided benchmark compares three ways to concatenate arrays in JavaScript: concat()
, spread operator (...
), and push()
.
Options Compared
concat()
: The traditional way to concatenate arrays using the concat()
method....
): A new feature introduced in ECMAScript 2015 that allows creating a new array by spreading elements from an existing array.push()
with spread operator (...
): Combining the push()
method with the spread operator to concatenate arrays.Pros and Cons
concat()
:...
):concat()
.push()
with spread operator (...
):concat()
.Library Use
None of the test cases use external libraries.
Special JavaScript Features or Syntax
The tests do not include any special JavaScript features or syntax that would affect their execution. However, it's worth noting that the spread operator (...
) is a feature introduced in ECMAScript 2015 and might not be supported by older browsers or versions of JavaScript.
Benchmark Preparation Code
There is no preparation code provided for each test case.
Latest Benchmark Results
The latest results show:
push()
: The fastest execution per second (15,1940.125), indicating that it is the most efficient method among the three options....
): The second-fastest, with an execution rate of 155,1940.125 seconds.concat()
: The slowest option, with an execution rate of 200,2007.25 seconds.Other Alternatives
If you need to concatenate arrays in JavaScript, other alternatives could be:
Array.prototype.push.apply()
: Similar to the spread operator, but with a function call instead of syntax.Array.prototype.concat()
with an array literal: Creating a new array and concatenating elements using an array literal.Keep in mind that these alternatives might have different performance characteristics compared to the options tested in this benchmark.