Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs forEach vs for vs map reduce
(version: 0)
Comparing performance of:
_.sumBy vs foreach vs for vs map reduce
Created:
3 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;
map reduce
arr.reduce((prev, next) => prev + next.value, 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.sumBy
foreach
for
map reduce
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 provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares four different approaches to summing up values in an array: 1. `_.sumBy` (using Lodash) 2. `forEach` 3. `for` loop 4. `map reduce` Each test case uses a random array of 100,000 objects with random integer values between 0 and 100. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a lot of functional programming helpers, including `sumBy`. The `sumBy` function takes an array and a function as arguments, where the function returns a value to be summed up. In this case, the function is simply returning the `value` property of each object in the array. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes used in this benchmark. All implementations use standard JavaScript constructs. **Test Case Comparison** Here's what's being tested: * `_.sumBy`: This test case uses Lodash's `sumBy` function to sum up all values in the array. * `forEach`: This test case uses a traditional `forEach` loop to iterate over the array and add up all values. * `for`: This test case uses a traditional `for` loop to iterate over the array and add up all values. * `map reduce`: This test case uses the `reduce` function to sum up all values in the array. **Options Compared** The four options are compared in terms of their execution performance. The goal is to determine which approach is the fastest for this specific use case. **Pros and Cons of Each Approach** Here's a brief summary: * `_.sumBy`: Pros: concise, easy to read; Cons: dependent on Lodash library, may have overhead due to function calls. * `forEach`: Pros: simple, easy to understand; Cons: can be slower than other approaches due to loop overhead. * `for`: Pros: straightforward, efficient; Cons: can be verbose and hard to read for complex loops. * `map reduce`: Pros: concise, can be parallelized; Cons: may have higher overhead due to function calls and array manipulation. **Other Considerations** * The benchmark uses a random array of values, which may not accurately represent real-world use cases. In reality, the data might be more structured or have specific properties that affect performance. * Lodash's `sumBy` function has some additional features, such as handling null or undefined values, which are not shown in this benchmark. **Alternatives** Other approaches to summing up array values might include: * Using a built-in JavaScript function like `Array.prototype.reduce()` * Using a specialized library like NumJS or Mathjs * Implementing a custom loop using bitwise operations or other optimized techniques Keep in mind that the best approach depends on the specific use case and performance requirements.
Related benchmarks:
Lodash sumBy vs map and reduce
Lodash sumBy vs forEach vs for
Lodash sumBy vs forEach vs for 2
Lodash sumBy (value and map) vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?