Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loop options performance
(version: 0)
Comparing performance of:
flatMap vs reduce vs for of loop vs filter and map vs forEach
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({length:100000}, (v, i) => ({name: i, shouldNotBeFiltered: i % 2 === 0}));
Tests:
flatMap
arr.flatMap(({ name, shouldNotBeFiltered }) => (shouldNotBeFiltered ? [name] : []));
reduce
arr.reduce((acc, { name, shouldNotBeFiltered }) => { if (shouldNotBeFiltered) { const elementToPush = name; acc.push(elementToPush); } return acc; }, []);
for of loop
const result = []; for (const { name, shouldNotBeFiltered } of arr) { if (shouldNotBeFiltered) { const elementToPush = name; result.push(elementToPush); } }
filter and map
arr.filter(({ shouldNotBeFiltered }) => shouldNotBeFiltered).map(({ name }) => name);
forEach
const result = []; arr.forEach(({ name, shouldNotBeFiltered }) => { if (shouldNotBeFiltered) { const elementToPush = name; result.push(elementToPush); } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
flatMap
reduce
for of loop
filter and map
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/125.0.0.0 Safari/537.36 Edg/125.0.0.0
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
flatMap
401.2 Ops/sec
reduce
742.0 Ops/sec
for of loop
1640.3 Ops/sec
filter and map
456.3 Ops/sec
forEach
1266.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. **Benchmark Definition and Script Preparation Code** The benchmark measures the performance of different loop options in JavaScript. The script preparation code creates an array `arr` with 100,000 elements, each containing `name` and `shouldNotBeFiltered` properties. The purpose of this array is to serve as a test data structure for the various loop options. **Loop Options Compared** The benchmark compares four different loop options: 1. **FlatMap**: This option uses the `flatMap` method to create a new array with the filtered elements. 2. **Reduce**: This option uses the `reduce` method to accumulate the filtered elements in an accumulator array. 3. **For of loop**: This option uses a traditional for loop to iterate over the array and push filtered elements into a result array. 4. **Filter and Map**: This option uses the `filter` method to create a new array with only the filtered elements, followed by the `map` method to create a new array with the names of the filtered elements. **Pros and Cons** Here's a brief summary of each loop option: 1. **FlatMap**: * Pros: Concise syntax, efficient implementation. * Cons: May not be as readable for complex data structures or nested loops. 2. **Reduce**: * Pros: Can handle complex data structures and accumulations, more flexible than `flatMap`. * Cons: May require more setup and understanding of the accumulator array. 3. **For of loop**: * Pros: Trivial to understand and implement, can be used with any data structure. * Cons: May be less efficient due to overhead of iterating over each element individually. 4. **Filter and Map**: * Pros: Easy to read and understand, can be chained together for complex filtering. * Cons: May not be as efficient as `flatMap` or `reduce`, requires two separate method calls. **Library Usage** The benchmark uses the built-in JavaScript Array methods: `flatMap`, `reduce`, `filter`, and `map`. These methods are part of the ECMAScript standard and do not require any external libraries. **Special JS Features or Syntax** None mentioned in this specific benchmark. However, some loop options may rely on special features like: * Iterators and generators (e.g., for loops with iterators) * Async/await syntax * Promises These features are not explicitly used in the provided benchmark code. **Alternatives** If you're looking for alternative loop options or methods, consider the following: 1. **Higher-order functions**: Instead of `flatMap` and `map`, use higher-order functions like `forEach` or `every`. 2. **Async/await syntax**: Use async/await to simplify looping over asynchronous operations. 3. **Closures**: Utilize closures to create private loops that can be reused throughout your codebase. Keep in mind that the choice of loop option ultimately depends on your specific use case, performance requirements, and coding style preferences.
Related benchmarks:
array last element big data
For with variable
Speed of Arr.length and Slice
Traditional vs For Each Loop
For Loop Leng Inside and Outside
Comments
Confirm delete:
Do you really want to delete benchmark?