Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs reduce v0.0.1
(version: 0)
Comparing performance of:
_.sumBy vs map and reduce
Created:
one year 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") / 1000;
map and reduce
arr.reduce((prev, next) => prev + next.value, 0) / 1000;
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy
1223.9 Ops/sec
map and reduce
2072.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark is comparing two approaches to calculate the sum of an array: `lodash.sumBy` and the `reduce` method with map (in this case, simply accessing the "value" property of each object in the array). **Options Compared** There are only two options being compared: 1. `_.sumBy(arr, "value") / 1000`: This option uses the `lodash.sumBy` function to calculate the sum of the "value" property of each object in the array. 2. `arr.reduce((prev, next) => prev + next.value, 0) / 1000`: This option uses the built-in `reduce` method with a callback function that adds up the values. **Pros and Cons** * **_.sumBy**: + Pros: Probably more concise and readable, as it's a specialized function designed to handle this specific use case. + Cons: It might be slower due to the overhead of using an external library or function call. However, `lodash` is a widely-used and optimized library, so this should be negligible. * **Reduce**: + Pros: Built-in to JavaScript, which means no additional dependencies or overhead. The syntax can also be more explicit and easier to understand for some developers. + Cons: Might require more code to achieve the same result (i.e., `arr.reduce((prev, next) => prev + next.value, 0)`). **Library and Purpose** `lodash` is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array operations, and more. In this case, `_sumBy` is a function from the `lodash` library that calculates the sum of a property across an array. **Special JS Feature or Syntax** None mentioned in the benchmark definition. **Other Considerations** When choosing between these two approaches, consider the following: * If you're working with large datasets or performance-critical code, `_.sumBy` might be a better choice due to its optimized implementation and reduced overhead. * If you prefer a more explicit and built-in approach, `reduce` is a good option. However, it may require more code and setup. **Alternative Approaches** Other alternatives for calculating the sum of an array in JavaScript include: * Using the `every()` method with a callback function: `arr.every((obj) => { return obj.value !== undefined; }, 0)` + This approach is less efficient than `reduce` or `_sumBy`, as it requires checking each object's existence before adding its value. * Using a `for...of` loop and explicit indexing: `let sum = 0; for (const obj of arr) { sum += obj.value; }` + This approach is straightforward but might be slower than the other two options due to the overhead of manual looping. Keep in mind that these alternatives may not be as concise or readable as using built-in functions like `_sumBy` or `reduce`.
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?