var param = [ 3 ];
var other = [ 1, 2 ].concat(param);
var param = [ 3 ]
var other = [ 1, 2, param ]
var param = 3;
var other = [ 1, 2 ].push(param);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 8292588.5 Ops/sec |
spread operator | 35842940.0 Ops/sec |
Push | 41053036.0 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares three ways to concatenate or manipulate arrays in JavaScript:
push
method...
)concat()
methodLibrary Used
In this benchmark, the Array.prototype.concat()
method is used as a library function, which means it's being tested against other implementations of array concatenation.
Special JS Feature or Syntax
The benchmark uses the ES6 spread operator (...
), which is a relatively new feature introduced in JavaScript. The spread operator allows you to expand an iterable (such as an array) into individual elements, and can be used to create new arrays by spreading existing arrays.
Options Compared
Here's a brief overview of each option being compared:
push()
method: This is the traditional way to add elements to an array in JavaScript. It modifies the original array....
): As mentioned earlier, this allows you to expand an iterable into individual elements and create new arrays by spreading existing arrays.concat()
method: This method returns a new array that is a copy of the original array, with additional elements added at the end.Pros and Cons
Here's a brief summary of the pros and cons of each option:
push()
method:...
):concat()
for simple cases.push()
or concat()
.concat()
method:push()
for large datasets.Benchmark Results
The latest benchmark results show that:
push()
method is the fastest option on this benchmark, followed closely by the concat()
method....
) is slower than both push()
and concat()
, likely due to its newer feature status and potential overhead.Other Alternatives
If you're looking for alternative ways to concatenate or manipulate arrays in JavaScript, here are a few options:
Array.prototype.splice()
method: This method modifies the original array by replacing elements at a specified index. It's similar to push()
, but with more flexibility.Array.prototype.slice()
method: This method returns a shallow copy of a portion of an array. It can be used in conjunction with other methods, such as concat()
, to create new arrays.Buffer
or TypedArray
: For specific use cases, you may want to consider using native libraries for optimized performance and memory usage.Keep in mind that these alternatives may have different trade-offs and requirements compared to the original options being tested in this benchmark.