var arr1 = [];
var arr2 = [];
for(let i=0; i<10; ++i){
arr1.push("phantasy")
arr1.push("joesguns")
arr1.push("max-hardware")
arr1.push("sigma-cosmetics")
arr1.push("harakiri-sushi")
arr1.push("foodnstuff")
arr1.push("hong-fang-tea")
arr1.push("iron-gym")
arr2.push("nectar-net")
arr2.push("omega-net")
arr2.push("silver-helix")
arr2.push("zer0")
arr2.push("n00dles")
arr2.push("neo-net")
arr2.push("the-hub")
arr2.push("crush-fitness")
}
var other = arr1.concat(arr2);
var arr1 = [];
var arr2 = [];
for(let i=0; i<10; ++i){
arr1.push("phantasy")
arr1.push("joesguns")
arr1.push("max-hardware")
arr1.push("sigma-cosmetics")
arr1.push("harakiri-sushi")
arr1.push("foodnstuff")
arr1.push("hong-fang-tea")
arr1.push("iron-gym")
arr2.push("nectar-net")
arr2.push("omega-net")
arr2.push("silver-helix")
arr2.push("zer0")
arr2.push("n00dles")
arr2.push("neo-net")
arr2.push("the-hub")
arr2.push("crush-fitness")
}
var other = arr1.push(arr2);
var arr1 = [];
var arr2 = [];
for(let i=0; i<10; ++i){
arr1.push("phantasy")
arr1.push("joesguns")
arr1.push("max-hardware")
arr1.push("sigma-cosmetics")
arr1.push("harakiri-sushi")
arr1.push("foodnstuff")
arr1.push("hong-fang-tea")
arr1.push("iron-gym")
arr2.push("nectar-net")
arr2.push("omega-net")
arr2.push("silver-helix")
arr2.push("zer0")
arr2.push("n00dles")
arr2.push("neo-net")
arr2.push("the-hub")
arr2.push("crush-fitness")
}
var other = [arr1, arr2];
var arr1 = [];
var arr2 = [];
for(let i=0; i<10; ++i){
arr1.push("phantasy")
arr1.push("joesguns")
arr1.push("max-hardware")
arr1.push("sigma-cosmetics")
arr1.push("harakiri-sushi")
arr1.push("foodnstuff")
arr1.push("hong-fang-tea")
arr1.push("iron-gym")
arr2.push("nectar-net")
arr2.push("omega-net")
arr2.push("silver-helix")
arr2.push("zer0")
arr2.push("n00dles")
arr2.push("neo-net")
arr2.push("the-hub")
arr2.push("crush-fitness")
}
var other = arr1.push.apply(arr1, arr2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
Push | |
Spread | |
push.apply |
Test name | Executions per second |
---|---|
Array.prototype.concat | 745020.2 Ops/sec |
Push | 697513.2 Ops/sec |
Spread | 336696.0 Ops/sec |
push.apply | 722093.6 Ops/sec |
Let's break down the provided benchmark.
Benchmark Overview
The benchmark compares the performance of four ways to concatenate arrays in JavaScript:
Array.prototype.concat()
...
)push()
with multiple argumentspush.apply()
The goal is to determine which approach is the fastest.
Options Compared
Here's a brief description of each option:
Array.prototype.concat()
: Concatenates two arrays using the concat()
method....
): Spreads one or more arrays into another array.push()
with multiple arguments: Pushes multiple elements onto an array using push()
.push()
on an array with multiple arguments.push()
, reducing overhead.Library Usage
None of the provided benchmark code uses any external libraries. However, it's worth noting that the spread operator (...
) is a feature introduced in ECMAScript 2015 (ES6), so support for this syntax may vary across browsers and environments.
Benchmark Results
The latest benchmark results show the performance of each option:
Array.prototype.concat()
: ~1019056.5 executions per secondpush.apply()
: ~887458.4375 executions per second...
): ~748621.6875 executions per secondpush()
with multiple arguments: ~902472.5 executions per secondBased on these results, using push()
with multiple arguments appears to be the fastest option, followed closely by Array.prototype.concat()
. The spread operator and push.apply()
are slower due to their respective syntax requirements and additional function calls.
However, it's essential to consider other factors that might affect performance, such as:
In conclusion, while the benchmark results provide a general idea of which options are faster, it's crucial to evaluate each approach in the context of your specific application and performance requirements.