Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter & Map vs Reduce 2021 v2
(version: 1)
Comparing performance of:
Filter & Map case 1 vs Reduce case 1 vs Reduce case 2
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var data = Array(10 ** 4).fill(null).map((_, i) => { return { checked: Math.random() < 0.5, val: Math.floor(Math.random() * 10000) } }) function someFn(i) { return (i * 3 * 8 / 1200 * 0.002 / 40 * 0.2); }
Tests:
Filter & Map case 1
data.filter(el => el.checked).map(el => someFn(el.val))
Reduce case 1
data.reduce((prev, curr) => { return curr.checked ? (prev.push(someFn(curr.val)), prev) : prev }, [])
Reduce case 2
data.reduce((prev, curr) => { return curr.checked ? [...prev, someFn(curr.val)] : prev }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Filter & Map case 1
Reduce case 1
Reduce case 2
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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare three different approaches for filtering, mapping, and reducing a large dataset in JavaScript. **Script Preparation Code** The script preparation code generates an array of 10,000 objects with random values for `checked` and `val`. The `someFn` function is defined to calculate a specific value based on the input `val`. **Test Cases** There are three test cases: 1. **Filter & Map case 1**: This test case filters the data using the `filter()` method, then maps each filtered element to its corresponding calculated value using the `map()` method. 2. **Reduce case 1**: This test case uses the `reduce()` method with an initial value of an empty array. It iterates over the data, pushing the calculated values into the array whenever a checked element is encountered. 3. **Reduce case 2**: This test case modifies the previous Reduce approach to push a new value (the result of someFn) instead of just pushing the original value. **Options Being Compared** The benchmark compares the performance of three approaches: 1. **Filter & Map**: This approach uses the `filter()` and `map()` methods in sequence. 2. **Reduce**: This approach uses the `reduce()` method with an initial value of an empty array. 3. **Modified Reduce**: This is a variant of the Reduce approach, where a new value (the result of someFn) is pushed instead of just the original value. **Pros and Cons** Here are some pros and cons for each approach: 1. **Filter & Map**: * Pros: Easy to understand and implement, linear time complexity. * Cons: Can be slower due to the overhead of filtering and mapping individual elements. 2. **Reduce**: * Pros: Efficient use of memory, can be faster than Filter & Map for large datasets. * Cons: Requires an initial value, can be confusing to understand for beginners. 3. **Modified Reduce**: * Pros: Similar to Reduce but with a slight performance boost due to the extra calculation. * Cons: May not be as intuitive or efficient for smaller datasets. **Library and Special JS Features** The benchmark uses: 1. **Array.prototype.filter()**: This method is used to filter elements in the array. 2. **Array.prototype.map()**: This method is used to create a new array with transformed values. 3. **Array.prototype.reduce()**: This method is used to reduce the array to a single value. **Special JS Features** The benchmark uses: 1. **Arrow functions**: The `someFn` function and the filter/mapper/reducer callback functions use arrow functions, which are a shorthand way of defining small functions in JavaScript. 2. **Template literals**: The script preparation code uses template literals to create a string with multiple lines. **Other Alternatives** If you're interested in exploring alternative approaches or libraries for this benchmark, here are some options: 1. **Lodash**: This popular library provides a `filter()` and `map()` function as well as an implementation of the `reduce()` method. 2. **Ramda**: Another functional programming library that provides a similar set of functions to Lodash. 3. **Closure libraries**: Libraries like Closure Compiler or Google's Closure Library can provide optimized implementations of these functions. Keep in mind that these alternatives may not provide identical results or performance characteristics, but they can offer different trade-offs and features depending on your specific use case.
Related benchmarks:
filter-map vs reduce 2
filter + map vs reduce 123
flatMap vs reduce vs filter.map
flatMap vs reduce vs filter.map v2
Flat map + filter vs. Reduce
Comments
Confirm delete:
Do you really want to delete benchmark?