Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Benchmark: flatMap vs reduce vs while vs foreach vs for of
(version: 5)
Comparing performance of:
flatMap vs reduce vs while vs foreach vs for vs for of
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var n = Array.from(Array(1000).keys())
Tests:
flatMap
n.flatMap((x,i) => [++i,i++])
reduce
n.reduce((acc, cur) => [...acc, ++cur, cur++], [])
while
let res = [], i=0; while(n.length>i++) res.push(i, i);
foreach
let res = []; n.forEach((_,i) => res.push(i+1, i+1))
for
let res = []; for (let i = 0; i < n.length; i++) { res.push(i+1, i+1) }
for of
let res = []; for (const i of n) { res.push(i+1, i+1) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
flatMap
reduce
while
foreach
for
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
flatMap
42257.7 Ops/sec
reduce
1627.9 Ops/sec
while
274578.5 Ops/sec
foreach
325829.6 Ops/sec
for
282628.7 Ops/sec
for of
337899.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Definition:** The benchmark is comparing four different approaches to iterate over an array: 1. `flatMap` 2. `reduce` 3. `while` loop 4. `forEach` loop 5. `for...of` loop Each of these approaches is being compared for performance, which means the test is measuring how fast each method can execute a specific code snippet. **Options Compared:** * `flatMap`: Flattens an array by mapping over it and concatenating the results. * `reduce`: Accumulates values in an array by reducing them using a callback function. * `while` loop: Uses a conditional statement to repeatedly execute a block of code. * `forEach` loop: Iterates over an array using a callback function for each element. * `for...of` loop: Iterates over an array using a more concise syntax. **Pros and Cons of Each Approach:** * `flatMap`: Pros: + Can be faster due to reduced overhead from concatenation. + More functional programming style. + May be more readable in certain contexts. Cons: + Can be less efficient for large arrays due to the mapping operation. * `reduce`: Pros: + Can be faster for reducing values in an array. + More concise and expressive than traditional loops. Cons: + May require more setup and understanding of callback functions. + Not as intuitive for simple iteration tasks. * `while` loop: Pros: + Allows for manual control over the iteration process. + Can be more efficient for specific use cases (e.g., looping until a condition is met). Cons: + More verbose than other options. + Requires explicit loop management. * `forEach` loop: Pros: + Concise and easy to read. + Works with most modern browsers and environments. Cons: + Less flexible than traditional loops. + May have performance issues for very large arrays. * `for...of` loop: Pros: + More concise and expressive than traditional loops. + Allows for more control over the iteration process. **Library Usage:** None of the test cases use any external libraries. The code snippets are self-contained and only rely on built-in JavaScript features. **Special JS Features or Syntax:** There is no special JavaScript feature or syntax being tested in this benchmark. **Other Considerations:** * Browser support: The benchmark is running on a Chrome mobile browser, which might have specific performance characteristics. * Device platform: The test is running on an Android device, which could affect the results due to differences in hardware and software configuration. * Environment: The benchmark is executed in a headless environment, which might not accurately reflect real-world use cases. **Alternatives:** Other alternatives for iterating over arrays include: * Traditional `for` loops * Array methods like `map()`, `filter()`, and `every()` * Higher-order functions like `Array.prototype.reduce()` and `Array.prototype.every()` Keep in mind that the choice of iteration method depends on the specific requirements of your use case.
Related benchmarks:
Benchmark: flatMap vs reduce vs while vs foreach
Benchmark: flatMap vs reduce vs while vs foreach (40k)
Reduce Push vs. flatMap with subarrays
flatMap vs reduce vs loop filtering vs filter/map performance
Comments
Confirm delete:
Do you really want to delete benchmark?