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 = [ 1, 2 ].push(params);
var params = [ "hello", true, 7 ];
var other = [ 1, 2 ];
other.push(params[0]);
other.push(params[1]);
other.push(params[2]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push | |
single push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 3685863.0 Ops/sec |
spread operator | 25777298.0 Ops/sec |
Push | 17120542.0 Ops/sec |
single push | 29517440.0 Ops/sec |
Overview
The provided benchmark measures the performance of different ways to concatenate or push elements onto an array in JavaScript, specifically using the new ES6 spread operator (...
), traditional concat()
method, and push()
method.
Benchmark Definition
The benchmark definition json explains that it compares four approaches:
...
): A new feature introduced in ES6, allowing for spreading elements onto an array.push(...)
): Using the push()
method with the spread operator to add elements to an array.push()
method.Options Compared
The benchmark compares these four approaches:
...
):push(...)
):push()
calls.concat()
....
) offers a concise alternative with good performance, making it an attractive choice for most use cases.push(...)
), while avoiding creating a new array object, may introduce unnecessary overhead compared to other methods.Library
None of the benchmark test cases explicitly use any JavaScript libraries. However, if we assume that the script preparation and HTML preparation codes are included as part of the benchmark setup (not explicitly shown in this snippet), they might involve some utility functions or library imports.
Special JS Feature/Syntax
The benchmark makes use of a new feature introduced in ES6: the spread operator (...
). This allows for spreading elements onto an array using the syntax array.push(...elements)
.
Please note that if you're testing other JavaScript versions prior to ES6, the spread operator might not work as expected.