var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
var params = [ "hello", true, 7 ]
var other = [ 1, 2, params ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 7044651.0 Ops/sec |
spread operator | 31730898.0 Ops/sec |
I'll explain the benchmark in detail.
Benchmark Definition
The benchmark compares two ways to concatenate arrays in JavaScript: the traditional concat()
method and the new ES6 spread operator (...
). This is done using a simple test case where an array of strings, booleans, and numbers is passed as an argument to the concatenation operation.
Script Preparation Code and Html Preparation Code
Since there are no script or HTML preparation codes provided in the benchmark definition, this means that the test cases should be executed from scratch on each run. The test results will likely depend on various factors such as browser version, operating system, device platform, etc.
Individual Test Cases
The two test cases are:
var params = [ "hello", true, 7 ];
var other = [ 1, 2 ].concat(params);
This test case creates an array other
by concatenating the array [ 1, 2 ]
with another array params
. The concat()
method is a built-in method of the Array.prototype
.
var params = [ "hello", true, 7 ];
var other = [ 1, 2, ...params ];
This test case creates an array other
by using the spread operator (...
) to concatenate the elements of the array params
with another array [ 1, 2 ]
. The spread operator is a new feature introduced in ES6.
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
concat()
Library Used
None. The benchmark uses only built-in JavaScript features.
Special JS Features or Syntax
The spread operator (...
) is a new feature introduced in ES6, which allows for more concise syntax when working with arrays and objects.
Alternatives
If you want to run this benchmark on an older browser that doesn't support the spread operator, you could use alternative approaches such as:
Array.prototype.push()
method to concatenate elementsArray.prototype.join()
method to concatenate elements (although this would require additional work to join strings with commas)Keep in mind that these alternatives may introduce significant changes to the benchmark's behavior and results.