Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Arr Reduce vs push/map
(version: 0)
Comparing performance of:
Arr.reduce vs Push vs Map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = [] for (let i = 0; i < 1000; i++){arr.push(i)}
Tests:
Arr.reduce
const arr = [] for (let i = 0; i < 1000; i++){arr.push(i)} const newArr = arr.reduce((acc,curr) => [...acc, curr], [])
Push
const arr = [] for (let i = 0; i < 1000; i++){arr.push(i)} const newArr = [] arr.forEach((curr) => newArr.push(curr))
Map
const arr = [] for (let i = 0; i < 1000; i++){arr.push(i)} const newArr = arr.map((curr) => curr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Arr.reduce
Push
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Arr.reduce
1417.6 Ops/sec
Push
106629.6 Ops/sec
Map
205141.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros/cons. **Benchmark Definition** The provided benchmark definition uses JavaScript to create an array of 1000 elements using a `for` loop. It then performs three operations on this array: reducing it using `Array.prototype.reduce()`, pushing new elements onto the end of the array using `push()`, and mapping the original array using `Array.prototype.map()`. **Options Compared** The benchmark compares the performance of three different approaches: 1. **`Array.prototype.reduce()`**: This method reduces an array to a single value (in this case, an array) by iterating over its elements and applying a callback function. 2. **`push()` + `forEach()`**: This approach involves pushing new elements onto the end of the original array using `push()`, followed by looping through the original array using `forEach()` to add each element to a new array. 3. **`Array.prototype.map()`**: This method creates a new array with the results of applying a provided function on every element in this array. **Pros and Cons** * **`Array.prototype.reduce()`**: + Pros: Can be more efficient for arrays with a small number of elements, as it avoids creating an intermediate array. It's also more concise. + Cons: May not be suitable for larger arrays due to the overhead of iterating over all elements. * **`push()` + `forEach()`**: + Pros: Suitable for large arrays, as only a single pass is required. The intermediate array can grow dynamically, reducing memory usage. + Cons: Requires creating an additional array to store the results, which may be less efficient than using `reduce()`. * **`Array.prototype.map()`**: + Pros: Similar to `push()` + `forEach()`, as it creates a new array without modifying the original one. It's also concise. + Cons: May require more memory for large arrays, as an intermediate array is created. **Library Used** None of the provided benchmark code explicitly uses any JavaScript libraries. However, some browsers might include internal libraries that could affect the performance results. **Special JS Feature/Syntax** None mentioned in this specific benchmark definition. **Other Considerations** * **Array initialization**: The original array `arr` is initialized using a `for` loop and `push()`, which can be inefficient for large arrays. Alternative methods like `Array.from()` or `new Array(size)` might be more efficient. * **Callback function in `reduce()`**: The callback function passed to `reduce()` takes two arguments (`acc` and `curr`). This can lead to unexpected behavior if not handled correctly. **Alternatives** For this specific benchmark, other alternatives could include: 1. Using `Array.prototype.reduceRight()` for right-to-left iteration. 2. Comparing the performance of different array initialization methods (e.g., using a constructor function vs. `new Array(size)`). 3. Using parallel processing or concurrency to compare the performance of these operations on larger datasets. Keep in mind that the choice of approach depends on the specific use case and requirements.
Related benchmarks:
flatMap vs reduce using push
Reduce Push vs. flatMap with subarrays
flatMap vs Reduce with push - test
flatMap vs Reduce with push - test2
flatMap vs reduce, but without copying the array in each iteration
Comments
Confirm delete:
Do you really want to delete benchmark?