Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash map filter vs reduce
(version: 0)
Lodash map filter vs reduce
Comparing performance of:
filter & map vs reduce
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var arr = []; for (var i = 0; i < 12345; i++) { arr[i] = {item: i}; }
Tests:
filter & map
_.filter(_.map(arr, (_m => _m.item)), _f => _f > 15 && _f < 30)
reduce
_.reduce(arr, (_ar, _r) => { if(_r.item > 15 && _r.item < 30) { _ar.push(_r.item) } return _ar; }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter & map
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 break down the provided JSON benchmark definition and explain what's being tested, compared, and other considerations. **Benchmark Definition** The benchmark defines two separate tests: 1. `_.filter(_.map(arr, (_m => _m.item)), _f => _f > 15 && _f < 30)` 2. `_.reduce(arr, (_ar, _r) => {\r\n\tif(_r.item > 15 && _r.item < 30) {\r\n\t\t_ar.push(_r.item)\r\n\t}\r\n\treturn _ar;\r\n}, [])` These tests are using the Lodash library to manipulate an array `arr` containing objects with an `item` property. The goal is to compare the performance of two different approaches: filtering and mapping, versus reducing. **Lodash Library** Lodash is a JavaScript utility library that provides a wide range of functions for tasks such as: * Array manipulation (e.g., map, filter, reduce) * Object manipulation (e.g., merge, clone) * Function utility functions (e.g., curry, partial) * String manipulation (e.g., trim, capitalize) In this benchmark, Lodash is used to provide the `map`, `filter`, and `reduce` functions. **Comparison of Approaches** The two tests are comparing the performance of: 1. **Filtering and mapping**: This approach involves filtering the array using `_.filter()` and then mapping each element to a new value using `_.map()`. The resulting values are then filtered again using `_f => _f > 15 && _f < 30`. 2. **Reducing**: This approach involves reducing the array using `_.reduce()`, which accumulates the elements in an array until it reaches a specified callback function. **Pros and Cons of Each Approach** 1. Filtering and mapping: * Pros: Can be more readable, easier to understand for some developers. * Cons: May involve additional overhead due to filtering and mapping operations, potentially leading to slower performance. 2. Reducing: * Pros: Can be more efficient, as it avoids the overhead of multiple filtering and mapping operations. * Cons: May require more complex logic, making it harder to understand for some developers. **Other Considerations** 1. **Device platform**: The benchmark is run on a Mac OS X 10.15.7 desktop with Chrome 100 browser. 2. **Browser version**: The latest available browser version (Chrome 100) is used. 3. **Execution frequency per second**: This measures how often the test is executed within a second, which can impact performance. **Alternatives** Other alternatives for these operations include: * For filtering: Using `Array.prototype.filter()` or `filter()` from the `Underscore.js` library (which is similar to Lodash). * For mapping: Using `Array.prototype.map()` or `map()` from the `Underscore.js` library. * For reducing: Using `Array.prototype.reduce()` or `reduce()` from the `Underscore.js` library. However, it's worth noting that using built-in JavaScript functions (e.g., `Array.prototype.filter()`, `Array.prototype.map()`, and `Array.prototype.reduce()`) might be more efficient than relying on a utility library like Lodash.
Related benchmarks:
Native map & filter vs reduce with spread
Lodash map & filter vs reduce with push
Lodash map & filter vs reduce with push and desctructuring (10 000 samples )
Native map & filter vs reduce with push and desctructuring (10 000 samples )
Native map & filter vs reduce with push
Comments
Confirm delete:
Do you really want to delete benchmark?