Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs map and reduce
(version: 0)
Comparing performance of:
_.sumBy vs map and reduce
Created:
7 years ago
by:
Guest
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:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.sumBy
_.sumBy(arr,"value");
map and reduce
arr.map(item => item.value).reduce((prev, next) => prev + next);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sumBy
map and reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy
1461.9 Ops/sec
map and reduce
806.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance of two approaches to calculate the sum of values in an array: `Lodash sumBy` versus `map and reduce`. The test data consists of an array of 100,000 objects, each with a random value between 0 and 100. The `getRandomInt` function generates these random values. **Options Compared** There are two options being compared: 1. **_sumBy**: This is a function from the Lodash library that calculates the sum of a property in an array using a callback function. In this case, it's used with the `value` property. 2. **Map and Reduce**: This is a built-in JavaScript approach that uses the `map` method to create a new array with transformed values and then applies the `reduce` method to sum up these values. **Pros and Cons** * **_sumBy**: + Pros: Often faster than `map and reduce`, especially for smaller datasets, since it avoids creating an intermediate array. + Cons: Requires the Lodash library, which may add overhead. Also, its performance can degrade if the callback function is complex or expensive to compute. * **Map and Reduce**: + Pros: No external dependencies required, as it's a built-in JavaScript method. Can be more efficient for large datasets since it avoids creating an intermediate array. + Cons: Creates an additional array during the transformation process, which can lead to increased memory usage. **Other Considerations** * **Library Usage**: The test case uses the Lodash library for `_sumBy`. This is a popular utility library that provides many functional programming helpers. However, it does add overhead due to its complexity and size. * **JavaScript Features**: Neither of these approaches utilizes any special JavaScript features or syntax. **Alternative Approaches** Other alternatives to consider: 1. **Array.prototype.reduce() only**: Without using `map`, you can directly apply the reduction function to the original array. This would bypass creating an intermediate array, but might be slower for large datasets. 2. **Using a custom implementation**: Depending on your specific use case and performance requirements, you might want to consider implementing a custom sum-by algorithm that avoids both Lodash's overhead and the memory usage of `map` and `reduce`. In summary, this benchmark helps evaluate the performance of two approaches: `Lodash sumBy` versus `map and reduce`. The choice between these options depends on your specific requirements, such as performance needs and library dependencies.
Related benchmarks:
Lodash sumBy vs reduce
Lodash sumBy vs simple reduce
Lodash sumBy vs forEach vs for vs map reduce
Lodash sumBy (value and map) vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?