Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs reduce vs loop performance (version: 1)
(version: 0)
Comparing performance of:
flatMap vs reduce vs loop
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; }, []);
loop
const result = []; for (const { name, shouldNotBeFiltered } of arr) { if (shouldNotBeFiltered) { const elementToPush = name; result.push(elementToPush); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
flatMap
reduce
loop
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
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
flatMap
583.8 Ops/sec
reduce
2025.4 Ops/sec
loop
2816.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. **What is being tested?** The benchmark is comparing the performance of three different approaches to filter an array: 1. `flatMap` 2. `reduce` 3. `loop` (a traditional for loop) Each approach is designed to push elements from the filtered array into a result array. The tests aim to measure which approach is the most efficient. **Options compared:** The benchmark compares the performance of these three approaches: * **`flatMap`**: A built-in JavaScript method that flattens an array by mapping over it and returning an array with the results. * **`reduce`**: A built-in JavaScript method that applies a reduction function to each element in the array, accumulating a result. * **`loop`**: A traditional for loop that iterates over the filtered array and pushes elements into the result array. **Pros and Cons of each approach:** * **`flatMap`**: + Pros: - Most concise and readable implementation - Built-in method, so it's likely to be optimized by JavaScript engines + Cons: - May have overhead due to function call and return statements - Not as control-oriented as `reduce` or `loop` * **`reduce`**: + Pros: - More control-oriented than `flatMap`, allowing for custom accumulator logic - Built-in method, so it's likely to be optimized by JavaScript engines + Cons: - May have more overhead due to function call and return statements - Can be less readable if not used correctly * **`loop`**: + Pros: - Most control-oriented approach, allowing for fine-grained control over iteration - No function call or return statement overhead + Cons: - Least concise and readable implementation - May require more maintenance due to its verbosity **Library usage:** None of the provided benchmark code uses any external libraries. All implementations are pure JavaScript. **Special JS features or syntax:** The benchmark doesn't explicitly mention any special JavaScript features or syntax that might be relevant to this comparison (e.g., async/await, generator functions, etc.). The focus is on the performance difference between these three approaches. **Other alternatives:** If you'd like to explore alternative approaches, some additional options could include: * Using `Array.prototype.filter()` instead of `flatMap` or `reduce` * Implementing a custom filtering function using arrow functions or regular expressions * Using other libraries like Lodash or Underscore.js for array manipulation However, the benchmark is already focused on comparing these three core approaches (`flatMap`, `reduce`, and `loop`), which should provide a good starting point for understanding performance differences.
Related benchmarks:
flatMap vs reduce filtering performance
flatMap vs reduce vs loop filtering performance
flatMap vs reduce vs loop filtering vs filter/map performance
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?