start = 0
stop = 10000
x = []
for(var i = 0; i < 10000; i++) {
x.push(i)
}
y = []
x.forEach(e => y.push(e))
y = []
y = y.push(x)
y = []
y = [y, x]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Push single | |
Push spread | |
Total spread |
Test name | Executions per second |
---|---|
Push single | 691.6 Ops/sec |
Push spread | 44649.6 Ops/sec |
Total spread | 26947.0 Ops/sec |
I'd be happy to help explain the JavaScript microbenchmark on MeasureThat.net.
Benchmark Overview
The benchmark measures the performance of three different approaches for extending an array in JavaScript:
forEach
loop with push
...
) with push
[...y, ...x]
)Approaches and their pros/cons
forEach
loop with push
This approach iterates over the array using a forEach
loop and pushes each element to a new array.
Pros:
Cons:
forEach
loop2. Spread operator (...
) with push
This approach uses the spread operator to create a new array by spreading the elements of the original array into a new array.
Pros:
forEach
loop approachCons:
3. Array spread operator ([...y, ...x]
)
This approach uses the array spread operator to create a new array by spreading the elements of both arrays into one.
Pros:
Cons:
Library and Special JS Features
There is no library mentioned in the benchmark. However, it's worth noting that some browsers may use specialized JavaScript engines or optimizations that can affect the results of the benchmark.
None of the approaches rely on any special JavaScript features beyond the standard forEach
loop and array spread operators.
Other Alternatives
If you need to extend an array, there are other approaches you could consider:
each
functionArray.prototype.pushAll
)Keep in mind that the best approach will depend on your specific use case and requirements. The MeasureThat.net benchmark provides a useful starting point, but you may need to experiment with different approaches to find the one that works best for your needs.