var value = 7;
var arr = [1, 2];
const other = arr.concat(value);
const other = [ arr, value ];
const other = [ 1, 2 ].push(value);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 1372329.1 Ops/sec |
spread operator | 2537640.5 Ops/sec |
Push | 4974816.0 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark that compares the performance of three methods for adding a single value to an array: Array.prototype.concat()
, the spread operator ([ ... ]
), and the push()
method.
Options Compared
[ ... ]
): This is a new feature introduced in ES6 that allows you to spread elements of an iterable (such as an array) into a new array. In this benchmark, it's used to add a single value to an existing array by spreading the existing array and then appending the new value.Pros and Cons
[ ... ]
):Library and Special JS Feature
In this benchmark, there is no library or special JavaScript feature being used beyond the standard ECMAScript features. The tests only rely on the built-in Array
prototype methods.
No special JavaScript features like async/await
, let
, const
, etc., are mentioned in the provided code snippets.
Other Alternatives
If you're looking for alternative approaches, consider the following:
concat
function that can be faster and more efficient than the built-in method.Keep in mind that these alternatives might come with additional overhead, dependencies, or trade-offs, so it's essential to evaluate their suitability based on your specific use case and performance requirements.