Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs for...of
(version: 0)
Comparing performance of:
flatMap vs for of vs forEach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
xs = new Array(1000).fill("world")
Tests:
flatMap
xs.flatMap((x) => ["hello", x])
for of
const out = [] for (const x of xs) { out.push("hello"); out.push(x); } return out
forEach
const out = [] xs.forEach( (x) => { out.push("hello"); out.push(x); }) return out;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
flatMap
for of
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
flatMap
33474.2 Ops/sec
for of
172641.9 Ops/sec
forEach
77270.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance difference between three approaches: `flatMap`, `for...of`, and `forEach` in JavaScript. **Script Preparation Code** The script preparation code is used to create an array `xs` with 1000 elements, each containing the string "world". This array will be used as input for the benchmark. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only tests the JavaScript code without any DOM-related overhead. **Test Cases** The benchmark consists of three test cases: 1. **`flatMap`**: Tests the `flatMap` method, which flattens an array and returns a new array with the results. 2. **`for...of`**: Tests the `for...of` loop, which iterates over an array and executes a block of code for each element. 3. **`forEach`**: Tests the `forEach` method, which iterates over an array and executes a callback function for each element. **Library Used** None of the test cases use any external libraries. **JavaScript Features/Syntax** * None of the test cases use any special JavaScript features or syntax. **Approach Comparison** The three approaches are compared in terms of their performance: 1. **`flatMap`**: This approach is likely to be faster because it uses a single iteration over the array and avoids creating multiple intermediate arrays. 2. **`for...of`**: This approach creates an additional array `out` to store the results, which may incur overhead due to array creation and push operations. 3. **`forEach`**: Similar to `for...of`, this approach also creates an additional array `out` to store the results. **Pros and Cons of Each Approach** * **`flatMap`**: + Pros: likely to be faster, fewer intermediate arrays created + Cons: may not work as expected if the inner array is large or complex * **`for...of`**: + Pros: can handle large or complex arrays, easy to understand and implement + Cons: creates an additional array `out`, potential overhead due to array creation and push operations * **`forEach`**: + Pros: similar to `for...of`, handles large or complex arrays well + Cons: also creates an additional array `out`, same potential overhead as `for...of` **Other Alternatives** In addition to these three approaches, other alternatives for iterating over an array in JavaScript include: 1. **`map`**: Similar to `flatMap`, but returns a new array instead of flattening the original array. 2. **`reduce`**: Accumulates values in an array and applies a callback function to each element. However, these alternatives may not be as suitable for this specific benchmark, which only tests the performance difference between three basic approaches.
Related benchmarks:
lodash.round VS Math.round
lodash.round VS Math.round (number to 2 decimal )
lodash.round VS Math.round (2 decimal places)
parsefloattofixed vs round
fill test
Comments
Confirm delete:
Do you really want to delete benchmark?