Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sum vs reduce vs for loop (v3)
(version: 0)
Comparing performance of:
lodash sum vs Array reduce vs manual for loop
Created:
2 years ago
by:
Registered User
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>
Tests:
lodash sum
const object = [ { one: 'valueOne', two: 'valueTwo', threeNum: 50 }, { one: 'valueOne', two: 'valueTwo', threeNum: 60 }, { one: 'valueOne', two: 'valueTwo', threeNum: 40 } ]; const lodashSum = _.sum(object.map(obj => obj.threeNum || 0)); return lodashSum;
Array reduce
const object = [ { one: 'valueOne', two: 'valueTwo', threeNum: 50 }, { one: 'valueOne', two: 'valueTwo', threeNum: 60 }, { one: 'valueOne', two: 'valueTwo', threeNum: 40 } ]; const reduceSum = object.reduce((accNumber, obj) => { accNumber += (obj?.threeNum || 0); return accNumber; }, 0); return reduceSum;
manual for loop
const object = [ { one: 'valueOne', two: 'valueTwo', threeNum: 50 }, { one: 'valueOne', two: 'valueTwo', threeNum: 60 }, { one: 'valueOne', two: 'valueTwo', threeNum: 40 } ]; let manualSum = 0; for (let i = 0; i < object.length; i++) { manualSum += (object[i]?.threeNum || 0); } return manualSum;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash sum
Array reduce
manual for loop
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 dive into the world of JavaScript benchmarks! **What is being tested?** The provided benchmark tests three different approaches to calculate the sum of an array of objects: 1. **Lodash `sum` method**: Uses the `lodash.sum` function from the Lodash library, which iterates over the array and sums up the values. 2. **Array `reduce` method**: Uses the `reduce` method on the array, which also iterates over the elements and accumulates the sum. 3. **Manual for loop**: Uses a traditional for loop to iterate over the array and calculate the sum. **Options compared** The benchmark compares these three approaches: * **Pros of each approach:** + Lodash `sum` method: - Convenient and concise syntax - Fast execution, thanks to the optimized library implementation + Array `reduce` method: - Efficient use of built-in array methods - Can be more readable and expressive than manual loops + Manual for loop: - Control over iteration flow - No external dependencies (e.g., libraries) * **Cons of each approach:** + Lodash `sum` method: - Additional dependency on the Lodash library - May have performance overhead due to library initialization and execution + Array `reduce` method: - May be slower than manual loops for very small arrays or specific use cases - Requires knowledge of the reduce method's behavior and usage + Manual for loop: - More verbose and error-prone than other approaches - Can lead to performance issues if not implemented correctly **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functions for common tasks, such as array manipulation, string manipulation, and functional programming. In this benchmark, the `sum` method is used to calculate the sum of an array. The `lodash.sum` function iterates over the array and sums up the values using a simple loop implementation. This approach provides good performance and readability, making it a convenient choice for many use cases. **Special JS feature: none** There are no special JavaScript features or syntaxes used in these benchmarks beyond what is provided by the standard language specification. **Other alternatives** If you're interested in exploring alternative approaches to summing arrays, here are some options: * **Using `Array.prototype.reduce()`**: This method is similar to the array reduce method used in the benchmark, but without the dependency on a library. * **Implementing a custom sum function using `for...of` loop or `forEach` method**: These methods can provide control over the iteration flow and allow for more flexibility in implementing the sum logic. * **Using a third-party library like Ramda**: Ramda is another popular JavaScript library that provides functional programming utilities, including array manipulation functions. These alternatives may offer different trade-offs between performance, readability, and maintainability, depending on your specific use case and requirements.
Related benchmarks:
Lodash reduce with native reduce
Lodash reduce vs native
Lodash reduce vs native in for loop
lodash reduce vs native reduce
lodash reduce vs array reduce
Comments
Confirm delete:
Do you really want to delete benchmark?