Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs for
(version: 0)
Comparing performance of:
_.sumBy vs for
Created:
5 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");
for
let result = 0; for (index = 0; index < arr.length; index++) result += arr[index].value; return result;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sumBy
for
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 benchmark and explain what's being tested. **Overview** The benchmark compares two approaches to calculate the sum of values in an array: using Lodash's `sumBy` function versus a traditional `for` loop. The test case creates an array of 100,000 objects with random values between 0 and 100, and then measures how long it takes to execute each approach. **Options compared** There are two options being compared: 1. **Lodash's `sumBy` function**: This is a higher-order function that takes an array and a function as arguments. The function returns the sum of the values in the array where the function returns a truthy value. 2. **Traditional `for` loop**: This approach uses a manual loop to iterate over the array, adding up the values. **Pros and Cons** **Lodash's `sumBy` function:** Pros: * More concise and readable code * Less prone to errors due to the use of a higher-order function * Can be reused with different functions Cons: * External dependency on Lodash (adds complexity) * May have performance overhead due to the need to call the external function **Traditional `for` loop:** Pros: * No external dependencies or overhead * More control over the execution flow * Can be optimized for specific use cases Cons: * Longer and more verbose code * More prone to errors due to manual calculations **Other considerations** * The test case uses a random array of values, which can affect performance. In reality, the input data may be more predictable. * The benchmark only measures the execution time, but other factors like memory usage or cache performance may also impact performance. **Library and purpose** The Lodash library is used to provide a convenient function for calculating sums. Specifically, `sumBy` is a utility function that reduces an array of objects by applying a function to each object's value property. In this benchmark, using `sumBy` allows the test case to focus on comparing the execution time of the two approaches without having to implement the sum calculation manually. **Special JS feature or syntax** There are no special JavaScript features or syntax used in this benchmark. The code is standard ECMAScript 2015 (ES6) compliant and does not rely on any experimental or proprietary features. **Alternatives** Other alternatives for calculating sums include: * Using `Array.prototype.reduce()` instead of `sumBy` * Implementing a custom sum function using bitwise operations * Using a library like NumJS for numerical computations However, these alternatives are not included in the benchmark as they would require significant modifications to the test case and may not provide comparable results.
Related benchmarks:
Lodash sumBy vs forEach
Lodash sumBy vs forEach vs for
Lodash sumBy vs forEach vs for 2
Lodash.isArray vs Array.isArray (Lodash v4.17.15)
Comments
Confirm delete:
Do you really want to delete benchmark?