Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[2024] Lodash .sumBy vs. Reduce with / 1000
(version: 0)
Comparing performance of:
_.sumBy with /1000 vs reduce with /1000
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.21/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 with /1000
_.sumBy(arr,"value") / 1000;
reduce with /1000
arr.reduce((prev, next) => { const total = next || 0; return prev + total / 1000; }, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sumBy with /1000
reduce with /1000
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 with /1000
1269.0 Ops/sec
reduce with /1000
201.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Overview** The provided JSON represents a benchmark test case on MeasureThat.net, where users can compare the performance of two approaches: Lodash's `sumBy` function and the built-in `reduce` method in JavaScript. The test measures how long it takes to sum up the values in an array using these two methods. **Script Preparation Code** The script preparation code generates a random array of 100,000 objects, each with a randomly generated value between 0 and 100. This creates a large dataset for the benchmark. ```javascript 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) }); } ``` **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library, version 4.17.21, which provides the `sumBy` function. ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script> ``` **Test Cases** There are two individual test cases: 1. **_.sumBy with /1000**: This test case uses Lodash's `sumBy` function to sum up the values in the array, and then divides the result by 1000. ```javascript Benchmark Definition: _.sumBy(arr, "value") / 1000; ``` 2. **reduce with /1000**: This test case uses the built-in `reduce` method in JavaScript to sum up the values in the array, and then divides the result by 1000. ```javascript Benchmark Definition: arr.reduce((prev, next) => { const total = next || 0; return prev + total / 1000; }, 0); ``` **Pros and Cons of Each Approach** Here's a brief analysis of each approach: 1. **_.sumBy with Lodash**: This method is likely to be faster because it's implemented in C++ by the Lodash team, which provides a significant performance advantage over a JavaScript implementation. * Pros: Highly optimized, reliable, and well-tested. * Cons: Requires an additional library (Lodash), may not work as expected if Lodash is not included in the environment. 2. **reduce with built-in JavaScript**: This method is implemented in C++ by the V8 JavaScript engine and is highly optimized for performance. * Pros: No additional libraries are required, works well out-of-the-box. * Cons: May not be as reliable or consistent due to the complexity of the JavaScript engine. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions, including `sumBy`. The `sumBy` function takes two arguments: an array and a property path (in this case, "value"). It sums up the values in the array based on the specified property path. **Special JS Feature/Syntax** None mentioned in the provided code snippet. **Other Alternatives** If you need to sum up values in an array, you can also consider using other libraries or built-in methods: 1. **Array.prototype.reduce()**: This method is a part of the JavaScript standard library and provides similar functionality to the `reduce` method used in the benchmark. 2. **lodash.sum()**: If you don't need the `sumBy` function specifically, you can use the `sum()` function provided by Lodash instead. 3. **Manual iteration**: You can also write a simple loop to sum up values in an array using manual iteration. Keep in mind that the performance difference between these alternatives may be negligible unless you're dealing with very large datasets or performance-critical code.
Related benchmarks:
Lodash sumBy vs reduce
Lodash sumBy vs simple reduce
[2024] Lodash .sumBy vs. Reduce with / 10002
Lodash sumBy vs reduce v0.0.1
Comments
Confirm delete:
Do you really want to delete benchmark?