Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test array reduce
(version: 0)
Quick benchmark
Comparing performance of:
reduce with new array vs reduce with array mutation vs flatMap
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10_000).fill(0)
Tests:
reduce with new array
arr.reduce((acc, x) => [...acc, x], [])
reduce with array mutation
arr.reduce((acc, x) => { acc.push(x); return acc; }, [])
flatMap
arr.flatMap(x => [x])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce with new array
reduce with array mutation
flatMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce with new array
29.1 Ops/sec
reduce with array mutation
34480.6 Ops/sec
flatMap
5607.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmarking test case on MeasureThat.net. The test measures the performance of three different approaches to reduce an array in JavaScript: reducing an array with new elements, reducing an array by mutating it, and using `flatMap`. Each approach is executed multiple times, and the execution speed (in executions per second) is recorded for each browser version. **Options Compared** The three options compared are: 1. **Reduce with New Array**: This approach creates a new array to accumulate the results, without modifying the original array. 2. **Reduce with Array Mutation**: This approach modifies the original array by pushing elements onto it during the reduction process. 3. **flatMap**: This is a modern JavaScript method that returns a new array with the results of applying a given function to each element. **Pros and Cons of Each Approach** 1. **Reduce with New Array**: * Pros: Efficient, fast, and easy to understand. * Cons: Creates an additional array, which can be memory-intensive for large inputs. 2. **Reduce with Array Mutation**: * Pros: Memory-efficient, as it only modifies the original array. * Cons: Can lead to unexpected side effects if not used carefully, as the original array is modified in place. 3. **flatMap**: * Pros: Concise and expressive syntax, easy to understand for modern JavaScript developers. * Cons: Requires support for modern JavaScript features (ES6+), can be slower due to the creation of a new array. **Library and Special JS Features** The test uses the `Array.prototype.reduce()` method, which is a built-in JavaScript library. The `flatMap()` method is also a built-in JavaScript method, introduced in ES6. No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** If you need to reduce an array in JavaScript, there are other alternatives: 1. **`reduceRight()`**: Similar to `reduce()`, but starts with the last element of the array and iterates from right to left. 2. **`forEach()` and `push()`**: You can use a loop with `forEach()` to iterate over the array and push elements onto another array using `push()`. However, this approach is less efficient than `reduce()` or `flatMap()`. 3. **Third-party libraries or utilities**: Depending on your specific requirements, you might consider using a third-party library or utility that provides additional features for reducing arrays. In summary, the choice of approach depends on your specific use case and performance requirements. If you need to reduce an array in JavaScript, `reduce()` with new elements is often a good starting point, followed by `flatMap` if you're targeting modern browsers and want concise syntax.
Related benchmarks:
Stelian
Basic Array processing benchmark
Array processing benchmark
flat vs flatMap vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?