Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Ctrl: flatMap() vs filter().map() vs reduce() v2
(version: 0)
flatMap() vs filter() & map() vs reduce()
Comparing performance of:
filter & map vs flatMap vs reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
filter & map
arr.filter(x => x % 5 && x % 3).map(x => x/100)
flatMap
arr.flatMap(x => (x % 5 && x % 3) ? x/100 : [])
reduce
arr.reduce((newArray, x) => { if (x % 5 && x % 3) { newArray.push(x / 100) } return newArray }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
filter & map
flatMap
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark definition and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition:** The benchmark is testing three different approaches for filtering and transforming an array of numbers: 1. `flatMap()` 2. `filter()` + `map()` 3. `reduce()` **Script Preparation Code:** The script starts by creating a large array `arr` with values ranging from 0 to 100,000 using a while loop. **Html Preparation Code:** There is no HTML preparation code provided, which suggests that the benchmark is focused on JavaScript performance testing only. **Individual Test Cases:** ### Filter & Map * Benchmark Definition: ```javascript arr.filter(x => x % 5 && x % 3).map(x => x/100) ``` This test case uses the `filter()` method to remove numbers that are not divisible by both 5 and 3, and then applies the `map()` method to divide the remaining numbers by 100. ### FlatMap * Benchmark Definition: ```javascript arr.flatMap(x => (x % 5 && x % 3) ? x/100 : []) ``` This test case uses the `flatMap()` method to remove numbers that are not divisible by both 5 and 3, and then flattens the resulting array into a single-level array. ### Reduce * Benchmark Definition: ```javascript arr.reduce((newArray, x) => { if (x % 5 && x % 3) { newArray.push(x / 100) } return newArray }, []) ``` This test case uses the `reduce()` method to build a new array with numbers that are divisible by both 5 and 3, and then divides those numbers by 100. **Pros and Cons:** * `flatMap()`: This approach is concise and efficient, as it avoids creating intermediate arrays. However, it may not be supported in older browsers or JavaScript engines. * `filter()` + `map()`: This approach is widely supported, but it creates intermediate arrays, which can lead to performance issues for large datasets. * `reduce()`: This approach is efficient, as it only requires a single pass through the array. However, it may require more memory than the other approaches. **Library:** There is no library mentioned in the benchmark definition. **Special JS Feature or Syntax:** None of the test cases use any special JavaScript features or syntax beyond standard ES6+ syntax. **Other Considerations:** * The benchmark uses a large array to simulate real-world performance scenarios. * The test cases are designed to compare the performance of different approaches for filtering and transforming arrays. * The benchmark results can be used to compare the performance of different browsers, JavaScript engines, or devices. **Alternatives:** There are other alternatives for testing array transformations, such as: * Using `Array.prototype.forEach()` with a callback function * Using a custom iterator or generator function * Using a third-party library like Lodash or Ramda
Related benchmarks:
Array flatMap() vs filter().map()
flatMap vs reduce vs filter.map
flatMap() vs filter().map() Bruno
flatMap vs reduce vs filter.map v2
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?