Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[2024] Lodash .sumBy vs. Reduce with / 10002
(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; }, 0) / 1000;
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
1271.3 Ops/sec
reduce with /1000
236.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and other considerations. **Benchmark Overview** The benchmark is comparing two approaches to calculate the sum of an array: 1. Using Lodash's `sumBy` function, which applies a callback function to each element in the array and sums up the results. 2. Using the built-in `reduce` method with a custom callback function to achieve the same result. **Script Preparation Code** The script preparation code generates a random array of 100,000 objects with a `value` property, where each value is a random integer between 0 and 100. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash JavaScript library (version 4.17.21). **Individual Test Cases** There are two test cases: 1. `_.sumBy with /1000`: This test case uses Lodash's `sumBy` function to calculate the sum of the array elements, where each element is multiplied by a factor of 1000. 2. `reduce with /1000`: This test case uses the built-in `reduce` method with a custom callback function to achieve the same result as `_.sumBy`. **Comparison** The comparison being tested is between two approaches: * Lodash's `sumBy` function * The built-in `reduce` method Both approaches aim to calculate the sum of the array elements. However, they use different techniques and libraries. **Pros and Cons:** 1. **Lodash's `sumBy` function** * Pros: + More concise and readable code. + Provides additional features like handling null or undefined values, and more. * Cons: + Requires an external library (Lodash). + May have performance overhead due to the library's existence. 2. **Built-in `reduce` method** * Pros: + No additional libraries are required. + Can be optimized by the JavaScript engine for better performance. * Cons: + More verbose code compared to Lodash's `sumBy`. + May require more manual error handling. **Library: Lodash** Lodash is a popular JavaScript library that provides a set of utilities and functions for common tasks, such as array manipulation, string templating, and mathematical operations. In this benchmark, Lodash's `sumBy` function is used to calculate the sum of the array elements. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives:** If you want to avoid using an external library like Lodash, you can use other approaches to calculate the sum of an array, such as: * Using a `for` loop and manual indexing. * Using the `forEach` method with a callback function. * Using the `every` method and manual calculation. However, these alternatives may require more code and manual error handling, making them less efficient and less readable than using a library like Lodash or the built-in `reduce` method.
Related benchmarks:
Lodash sumBy vs reduce
Lodash sumBy vs simple reduce
[2024] Lodash .sumBy vs. Reduce with / 1000
Lodash sumBy vs reduce v0.0.1
Comments
Confirm delete:
Do you really want to delete benchmark?