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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 13819427.0 Ops/sec |
spread operator | 55409592.0 Ops/sec |
Push | 62919524.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided benchmark compares three different ways to concatenate or push elements onto an array in JavaScript:
Array.prototype.concat()
: The traditional way to concatenate two arrays using the concat()
method....
): The new ES6 spread operator, introduced in ECMAScript 2015, which allows for more concise and expressive array creation.Array.prototype.push()
with spread syntax (...
): A way to push elements onto an array using the push()
method with the spread operator.Options being compared
The benchmark is comparing these three options on the same input data:
params
: An array containing a string, a boolean, and a number.other
are created by concatenating or pushing params
onto them.Pros and Cons of each approach:
Array.prototype.concat()
:...
):concat()
, especially for large arrays.Array.prototype.push()
with spread syntax (...
):push()
) and may not be as efficient as the spread operator.Other considerations
Library and special JS features
None are mentioned in this specific benchmark. However, it's worth noting that other libraries or frameworks might use different approaches to array manipulation or provide additional features that could impact performance.
Now, if you'd like to know about some alternative ways to concatenate arrays in JavaScript, here are a few:
Array.prototype.reduce()
: Can be used to concatenate arrays by defining a custom reduce function.Array.prototype.map()
with ...
: Can be used to push elements onto an array using the spread operator.These alternatives might be worth exploring in different scenarios or for specific use cases.