var subarrs = [
[ {"name": "hello", "isGood": true},
{"name": "hello", "isGood": false},
{"name": "hello2", "isGood": true},
],
];
var other = [ {"name": "hello 3", "isGood": true} ]
subarrs.concat(other)
var other = [ {"name": "hello 3", "isGood": true} ]
subarrs.push(other)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
Push Spread |
Test name | Executions per second |
---|---|
Array.prototype.concat | 13640891.0 Ops/sec |
Push Spread | 13566876.0 Ops/sec |
The provided benchmark tests the performance of three different methods for combining arrays in JavaScript: the traditional Array.prototype.concat
method, the ES6 spread operator used with the push
method (array.push(...other)
), and the spread operator by itself (though it is not directly included in the tests as they are defined). The benchmark compares these methods in terms of execution speed.
Array.prototype.concat:
Push Spread (array.push(...other)
):
...
) and the push
method to add elements of another array into an existing array.Array.prototype.concat
method is part of the ECMAScript standard for all versions of JavaScript and is widely supported. The spread operator (...
) was introduced in ES6, so it requires a more modern environment for compatibility.In addition to concat
and push
with the spread operator, there are a few other methods and patterns for merging arrays:
[...array1, ...array2]
creates a new array combining the elements of the two arrays. This is similar to the push spread but focuses on creating a new array.unshift
method can be used, but it also modifies the original array._.concat
, which can extend the functionality and possibly improve performance depending on the use case.Overall, this benchmark evaluates the speed of different methods for combining arrays, providing insights for developers about optimal practices and performance considerations in JavaScript array manipulation.