random = () => Array(50).fill(Math.floor(Math.random() * 9))
arr1 = random();
arr2 = random();
const other = arr1.concat(arr2);
const other = [ arr1, arr2]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 270875.9 Ops/sec |
spread operator | 179211.3 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Description
The benchmark compares two approaches to concatenate arrays: the traditional Array.prototype.concat()
method and the new ES6 spread operator ([ ...arr1, ...arr2]
).
Options Compared
Two options are compared:
concat()
method: This is the legacy way of concatenating arrays in JavaScript.Pros and Cons
concat()
method:concat()
method because it avoids creating new arrays and uses a single array operation.Library Usage
There is no explicit library usage mentioned in this benchmark. However, if you're interested in knowing about libraries that can simplify array concatenation, some popular ones include:
_.concat()
): A utility library for functional programming.concat
function.Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark. Both approaches use standard JavaScript syntax and do not rely on any advanced features like async/await, promises, or ES6 modules.
Other Alternatives
If you want to compare array concatenation methods with other alternatives, consider the following:
Array.prototype.reduce()
instead of concat()
: This can be a more efficient approach in some cases.Array.prototype.push()
multiple times: This can be slower than using concat()
or the spread operator.For this specific benchmark, the ES6 spread operator is likely to be faster and more efficient due to its optimized implementation and avoidance of unnecessary array creations.