var array1 = [Array(1000).keys()]
var array2 = array1.map(i => i * 10)
const result = array1.concat(array2);
const result = [ array1, array2 ]
const result = array1.push(array2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 264782.2 Ops/sec |
spread operator | 75444.9 Ops/sec |
Push | 32985.1 Ops/sec |
What is tested on the provided JSON?
The provided JSON represents a JavaScript microbenchmark test case created using MeasureThat.net. The test compares three approaches for concatenating two arrays: Array.prototype.concat()
, the spread operator (...
), and the push()
method with multiple elements.
Options compared
...
): This operator spreads the elements of an iterable (such as an array) into a new array, allowing for concatenation in a concise way.push()
to add elements to the end of an array.Pros and cons of each approach
...
) concat()
due to the creation of an intermediate array; requires modern JavaScript engines.Library usage
None in this specific test case. However, if any of these approaches rely on libraries or external dependencies, they would need to be considered in the analysis.
Special JavaScript feature or syntax
The spread operator (...
) is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). It allows for concise array concatenation and spreading elements into new arrays. This test case likely relies on this feature to demonstrate its performance compared to traditional methods.
Other alternatives
push()
followed by the spread operator: [...array, ...array2]
. However, this approach still requires multiple push() calls and may not be as efficient as the spread operator alone.Array.from()
can also be used to create a new array from an iterable, but it's less common for concatenation use cases.Benchmarking considerations
By considering these factors, we can gain insights into the relative performance and efficiency of the three array concatenation approaches.