var list1 = [];
var list2 = [];
for(var i = 0; i<10000; i++) {list1.push(Math.random());list2.push(Math.random());}
var result = list1.concat(list2);
var list1 = [];
var list2 = [];
for(var i = 0; i<10000; i++) {list1.push(Math.random());list2.push(Math.random());}
var other = [ list1, list2 ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 576.4 Ops/sec |
spread operator | 565.4 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is designed to compare two approaches for concatenating arrays: the traditional Array.prototype.concat()
method and the new ES6 spread operator (...
).
Options Compared
Two options are compared:
Array.prototype.concat()
: This method creates a new array by copying elements from one or more source arrays....
): This operator is used to create a new array by spreading the elements of an existing array.Pros and Cons
Array.prototype.concat()
:...
):Library
There is no specific library being tested in this benchmark. The focus is on comparing two native JavaScript methods.
Special JS Feature/Syntax
The spread operator (...
) is a new feature introduced in ECMAScript 2015 (ES6). It allows for more concise and expressive code, but may require additional compiler or runtime support in older browsers or versions of JavaScript.
Benchmark Preparation Code
The provided script preparation code simply creates two empty arrays, list1
and list2
, and then populates them with random elements using a for
loop. The benchmark then calls the concat()
method on one array and uses the spread operator to concatenate the two arrays in another test case.
Other Alternatives
Before the spread operator was introduced, developers used other methods for concatenating arrays, such as:
Array.prototype.push()
method repeatedly: arr1.push(...arr2)
Array.from()
method and passing an array literal with spread syntax: [...arr1, ...arr2]
However, these alternatives are not being tested in this benchmark.
I hope this explanation helps!