Array(10000).fill(1).reduce((acc, i) => {acc.push(i); return acc;}, []);
Array(10000).fill(1).reduce((acc, i) => {return [acc, i]}, []);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Push | |
Spread |
Test name | Executions per second |
---|---|
Push | 24751.2 Ops/sec |
Spread | 28.9 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition:
The benchmark is comparing two approaches for reducing an array over its results:
Array(10000).fill(1).reduce((acc, i) => {acc.push(i); return acc;}, [])
: This uses the traditional .push()
method to add elements to the accumulator.Array(10000).fill(1).reduce((acc, i) => {return [...acc, i]}, [])
: This uses the new ES6 spread operator (...
) to create a new array with the accumulated values.Options Compared:
The benchmark is comparing two options:
.push()
method (traditional approach)...
) (new ES6 approach)Pros and Cons of Each Approach:
...
):Library:
None of the test cases use a specific library. The benchmark is focusing solely on the JavaScript built-in methods and syntax.
Special JS Feature or Syntax:
The test case uses the new ES6 spread operator (...
). This feature was introduced in ECMAScript 2015 (ES6) and allows for creating a new array with elements from an existing array.
Other Considerations:
Alternatives:
If you were to implement this benchmark yourself, you could consider using other JavaScript methods or libraries for reducing arrays over its results, such as:
Array.prototype.concat()
Lodash
library (e.g., lodash.forEach
)Underscore.js
library (e.g., _reduceRight
)Keep in mind that the performance differences between these alternatives may vary depending on the specific use case and browser/JavaScript engine being targeted.