var ys = [4, 5, 6];
var xs = [1, '2', '3'];
for (let i = 0; i < xs.length; i++) {
ys.push(xs[i]);
}
return ys;
return ys.push(xs);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For | |
Spread |
Test name | Executions per second |
---|---|
For | 843497.8 Ops/sec |
Spread | 3834789.5 Ops/sec |
Let's dive into the benchmark test cases and break down what's being tested, compared, and analyzed.
Benchmark Definition
The benchmark is titled "Pusshhhhh" with no description provided. The script preparation code initializes two arrays: ys
with integers [4, 5, 6]
and xs
with a mix of integers and strings ['2', '3']
.
Test Cases
There are two test cases:
for
loop to iterate over the elements of array xs
and push each element into array ys
.for (let i = 0; i < xs.length; i++) {
ys.push(xs[i]);
}
...
) to concatenate the elements of array xs
onto the end of array ys
. The resulting code is a one-liner:return ys.push(...xs);
Latest Benchmark Result
The benchmark results show two test runs:
What's Being Tested
In this benchmark, we're comparing two different approaches to concatenating arrays:
for
loop approach (used in the "For" test case)...
) approach (used in the "Spread" test case)Comparison of Options
Here are some key points to consider when choosing between these two approaches:
for
loop approach.Other Considerations
When deciding which approach to use:
for
loop approach for compatibility reasons.Library and Special JS Feature/Syntax
In this benchmark:
...
) is a special feature introduced in ECMAScript 2015 (ES6) that allows array concatenation using the ...
syntax.