var a = [1,2,3];
var b = [4,5,6];
var c = [a, b]
var a = [1,2,3];
var b = [4,5,6];
var c = a.concat(b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
concat |
Test name | Executions per second |
---|---|
spread | 862494.4 Ops/sec |
concat | 1337704.4 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition
The benchmark is testing the performance difference between two approaches: concatenating arrays using the concat()
method and spreading an array using the spread operator (...
).
Options Compared
There are two options compared:
concat()
method to concatenate two arrays, [a]
and [b]
. The resulting array is stored in variable c
....
) to create a new array by spreading the elements of both input arrays, [a]
and [b]
. The resulting array is also stored in variable c
.Pros and Cons
Library Used
There is no library used in this benchmark. The tests only use built-in JavaScript features.
Special JS Feature/ Syntax
There are no special JavaScript features or syntax used beyond what's required for the two options being compared.
Other Alternatives
If the spread operator were not available, an alternative could be using Array.prototype.push()
to concatenate arrays:
var c = []; c.push.apply(c, a); c.push.apply(c, b);
Another option would be using Array.prototype.reduce()
or Array.prototype.forEach()
to achieve similar results.
It's worth noting that in modern JavaScript, the spread operator is often preferred over concatenation because of its efficiency and simplicity.
Benchmark Preparation Code
The provided benchmark preparation code is empty. Typically, this code would include any necessary setup or initialization to prepare the test environment.
Latest Benchmark Result
The latest benchmark result shows the performance characteristics of both options for a specific browser (Firefox 83) on a desktop device running Ubuntu. The results indicate that spreading is generally faster than concatenation, which aligns with expectations based on the pros and cons discussed earlier.