var params = [ "hello", true, 7 ];
var params2 = [ 1, 2 ]
params2.concat(params);
var params = [ "hello", true, 7 ]
var params2 = [ 1, 2 ]
params2 = [ params2, params ]
var params = [ "hello", true, 7 ];
var params2 = [ 1, 2 ]
params2.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 | 3619795.8 Ops/sec |
spread operator | 11581827.0 Ops/sec |
Push | 12552856.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition and Purpose
The benchmark, "Array concat vs spread operator vs push new", compares three ways to concatenate arrays in JavaScript:
concat()
...
)push(...)
with multiple argumentsThe purpose of this benchmark is to determine which method is the most efficient.
Options Compared
We have three options being compared:
concat()
: A traditional method for concatenating arrays in JavaScript....
): A new ES6 feature that allows you to spread an array into multiple arguments.push(...)
with multiple arguments: A way to push elements onto an array using the spread operator.Pros and Cons of Each Approach
Here's a brief overview of each approach:
concat()
:...
):push(...)
with multiple arguments:concat()
for large arrays.Library Used
There is no explicit library mentioned in the benchmark definition or test cases. However, it's likely that the benchmark is running in a browser environment, which uses the V8 JavaScript engine.
Special JS Feature or Syntax
The ES6 spread operator (...
) is a special feature introduced in ECMAScript 2015. It allows you to spread an array into multiple arguments, making it more concise and efficient than traditional concatenation methods.
Benchmark Test Cases
Each test case consists of:
Test Name
: a descriptive name for the test case (e.g., "Array.prototype.concat").The three test cases are:
concat()
: Concatenates two arrays using the concat()
method....
): Concatenates two arrays using the spread operator.push(...)
with multiple arguments: Pushes elements onto an array using the spread operator.Latest Benchmark Results
The latest benchmark results show the execution speed of each test case in Chrome 83 on a Windows desktop:
Push
(Spread Operator): 125,528,560 executions per secondspread operator
: 115,818,270 executions per secondArray.prototype.concat
: 36,197,995 executions per secondThe results suggest that the push(...)
with multiple arguments and spread operator methods are faster than the traditional concat()
method.
Alternatives
Other alternatives for concatenating arrays in JavaScript include:
Array.prototype.push()
and then passing an array of elements: arr.push(...newArr)
Keep in mind that these alternatives may have different performance characteristics or use cases compared to the methods being benchmarked.