Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs forEach vs for
(version: 0)
Comparing performance of:
_.sumBy vs foreach 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");
foreach
let result = 0; arr.forEach(element => { result += element.value }); return result;
for
let result = 0; for(let i=0;i<arr.length;i++) { result += arr[i].value; }; return result;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.sumBy
foreach
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy
1544.2 Ops/sec
foreach
5252.9 Ops/sec
for
186.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test cases and explain what's being tested. **Benchmark Test Cases** The test cases are comparing three different approaches to sum up the values in an array: 1. **Lodash's `sumBy` function**: This function uses the `forEach` method internally, but with a few optimizations that make it more efficient than a simple `for` loop. 2. **`forEach` loop**: A traditional JavaScript `forEach` loop that iterates over the array and adds up the values. 3. **`for` loop**: A classic `for` loop that uses an index variable to iterate over the array and add up the values. **Options Compared** The three options are compared in terms of their performance, which is measured by the number of executions per second. **Pros and Cons** Here's a brief summary of each approach: * **Lodash's `sumBy` function**: + Pros: Optimized for performance, reduces boilerplate code. + Cons: Adds an external dependency (Lodash), may have a higher memory overhead due to the additional function call stack. * **`forEach` loop**: + Pros: Easy to understand and implement, no external dependencies. + Cons: May be slower than optimized loops like `sumBy`, as it relies on JavaScript's dynamic typing and method calls. * **`for` loop**: + Pros: Generally faster than `forEach` for simple iterations, low memory overhead. + Cons: More verbose and prone to errors due to index manipulation. **Library: Lodash** The `lodash.js` library is a popular utility library that provides various functional programming helpers, including the `sumBy` function. The `sumBy` function takes two arguments: an array and a callback function that extracts the value from each element. In this benchmark, the `lodash.min.js` file is included in the HTML preparation code to make the `sumBy` function available for testing. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in these test cases. The focus is on comparing the performance of different iteration approaches. **Other Alternatives** If you're interested in exploring alternative iteration methods, here are a few examples: * **`reduce()` method**: This method can be used to sum up an array by reducing it from right to left using a callback function. * **`Array.prototype.every()` and `Array.prototype.some()`**: These methods can be used with custom callback functions to filter or accumulate data in arrays. Keep in mind that these alternatives may have different performance characteristics, memory overhead, and readability trade-offs compared to the options tested in this benchmark.
Related benchmarks:
Lodash sumBy vs for
Lodash sumBy vs forEach
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?