var arr1 = ['a', 'b', 'c'];
var arr2 = ['d', 'e', 'f'];
arr1.concat(arr2)
[ arr1, arr2 ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array concat | |
ES6 spread operator |
Test name | Executions per second |
---|---|
Array concat | 1028234.0 Ops/sec |
ES6 spread operator | 1813151.1 Ops/sec |
Let's break down the benchmark definition and test cases to understand what is being tested.
Benchmark Definition:
The benchmark measures the performance difference between two approaches:
arr1.concat(arr2)
[ ...arr1, ...arr2 ]
(using the ES6 spread operator)These two approaches are used to concatenate two arrays, arr1
and arr2
. The main difference between them is how they handle the resulting array.
Pros and Cons of each approach:
arr1.concat(arr2)
arr1
, which can lead to unnecessary memory allocations.[ ...arr1, ...arr2 ]
(using ES6 spread operator)concat()
as it only creates a new array reference without creating a copy of the original arrays.Library:
In this benchmark, there is no explicit library mentioned. However, the use of ES6 spread operator ([ ...arr1, ...arr2 ]
) implies that the benchmark is using a modern JavaScript engine with ES6 support.
Special JS feature/syntax:
The benchmark uses the ES6 spread operator ([ ...arr1, ...arr2 ]
), which is a modern JavaScript syntax introduced in ECMAScript 2015. This syntax allows for concise array creation by expanding arrays or iterables.
Other alternatives:
For concatenating two arrays, other approaches include:
arr1.push(...arr2)
: This approach modifies the original arr1
and can be slower due to its mutability.arr1
using slice()
and then concatenates arr2
.These alternatives may have different performance characteristics, depending on the specific use case and browser support.
Benchmark Preparation Code:
The provided preparation code sets up two arrays, arr1
and arr2
, with three elements each. This allows for a straightforward test of the concatenation methods.
Latest Benchmark Result:
The latest benchmark result shows that Chrome 79 on Windows Desktop achieved:
[ ...arr1, ...arr2 ]
)arr1.concat(arr2)
This suggests that the ES6 spread operator outperforms the traditional concat()
method in this specific benchmark.