Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatten reduce vs for of
(version: 0)
Comparing performance of:
reduce vs for of
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
reduce
var flatten = (arr) => arr.reduce((acc, x) => { Array.isArray(x) ? flatten(x).forEach(y => acc.push(y)) : acc.push(x) return acc; }, []) const x = flatten([ [1,2,3], [ [4,5,6], [7,8,9], [[[[[10,11,12]]], [13,14,15]]] ], [[[[[[[[[[[16,17,18]]]]]]]]]]] ]);
for of
function flat(argarr) { const arr = [] for (item of argarr) { if (Array.isArray(item)) arr.push(flat(item)) else arr.push(item) } return arr } const x = flat([ [1,2,3], [ [4,5,6], [7,8,9], [[[[[10,11,12]]], [13,14,15]]] ], [[[[[[[[[[[16,17,18]]]]]]]]]]] ]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
201878.2 Ops/sec
for of
58945.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark explanation. **Benchmark Purpose** The benchmark tests two approaches to flatten an array: `reduce` and `for...of`. The goal is to compare their performance in flattening nested arrays. **Options Compared** 1. **Reduce**: This approach uses the `Array.prototype.reduce()` method, which applies a function to each element of an array (in this case, the nested arrays) cumulatively, from left-to-right, so as to reduce it to a single output value. 2. **For...of Loop**: This approach uses a traditional `for` loop with the `foreach` or `of` keyword to iterate over the elements of the nested array. **Pros and Cons** * **Reduce**: + Pros: concise code, built-in functionality, potentially faster execution. + Cons: can be less readable for complex arrays, may not work as expected if the initial array is not flat. * **For...of Loop**: + Pros: more explicit control over iteration, easier to understand for simple cases. + Cons: requires manual management of the loop (e.g., pushing elements to an accumulator array), potentially slower execution. **Library and Special JS Features** In this benchmark, there is no specific library used. However, note that `Array.prototype.reduce()` is a built-in JavaScript method, which means it's not explicitly imported or required like in some other contexts. There are no special JS features mentioned or tested in this benchmark. **Benchmark Preparation Code and Execution** The preparation code for each benchmark case is provided in the `Benchmark Definition` section. The execution results are stored in the `RawUAString`, `Browser`, `DevicePlatform`, `OperatingSystem`, `ExecutionsPerSecond`, and `TestName` fields of the latest benchmark result. **Alternative Approaches** Other possible approaches to flatten a nested array could include: 1. **Recursive Function**: A custom function that recursively calls itself with each sub-array element. 2. **Using `flat()` Method (if available)**: Some modern browsers and environments support the `Array.prototype.flat()` method, which can be used to flatten arrays in a single step. 3. **Manual Iteration**: Using traditional `for` loops or other iteration mechanisms to iterate over the array elements. Keep in mind that each approach has its trade-offs in terms of readability, performance, and maintainability.
Related benchmarks:
flatMap vs reduce using push
flatMap vs reduce (with concat())
flatMap vs reduces
Flatmap vs reduce with objects
flatMap vs reduce flattern array
Comments
Confirm delete:
Do you really want to delete benchmark?