var data = [Array.from(Array(10000).keys())]
data.reduce((acc, curr) => [acc, curr],[])
data.reduce((acc, curr) => (acc.push(curr),acc),[])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
push |
Test name | Executions per second |
---|---|
spread | 4.4 Ops/sec |
push | 18774.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark measures the performance difference between two approaches: using the push
method to add elements to an array, versus using the spread operator (...
) with the reduce
method.
Options Compared
Two options are compared:
push
method to add each element of the array to the end of the accumulator array....
) with the reduce
method to create a new array by spreading the elements of the input array into the accumulator array.Pros and Cons
Array.from()
is used) or object creation (for other spread implementations)Library
In this benchmark, Array.from()
is used to create an array of numbers from 0 to 9,999. This library is a built-in JavaScript function that creates a new array by mapping over the input iterable.
Special JS Feature or Syntax
The use of ...
(spread operator) and reduce()
is a modern JavaScript feature. The spread operator was introduced in ECMAScript 2015, and reduce()
has been part of the language since ECMAScript 2009.
Other Considerations
Other Alternatives
Some alternative approaches that could be tested in this benchmark include:
concat()
instead of push
These alternatives would require additional modifications to the benchmark definition and script preparation code.