var a = [ 1, 2, 3 ];
var b = [ 4, 5, 6 ];
var result = a.concat(b);
var result = [ a, b ];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 12998336.0 Ops/sec |
spread operator | 36495552.0 Ops/sec |
Let's dive into the explanation of the provided benchmark.
What is tested?
The benchmark tests two approaches for concatenating two arrays: Array.prototype.concat
and the spread operator ([ ...a, ...b ]
). The goal is to compare their performance in JavaScript microbenchmarks.
Options compared
Two options are being compared:
Array.prototype.concat
: A traditional method for concatenating arrays, which was part of the ECMAScript standard before the introduction of the spread operator.[ ...a, ...b ]
): A new syntax introduced in ECMAScript 2015 (ES6) that allows creating a new array by spreading elements from existing arrays.Pros and cons of each approach
Array.prototype.concat
:[ ...a, ...b ]
):concat
because it avoids the function call overhead.Library usage
In this benchmark, there is no explicit library usage mentioned. However, the spread operator syntax relies on the Array.prototype
object and the ...
spread syntax, which are built-in JavaScript features.
Special JS feature/syntax
The spread operator ([ ...a, ...b ]
) uses a new syntax introduced in ECMAScript 2015 (ES6). This syntax is not mentioned in older browsers or environments that don't support ES6+.