Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sum vs sumBy vs reduce
(version: 1)
Comparing performance of:
sum vs sumBy vs lodash sum vs lodash sumBy
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:
const smallArray = Array.from({ length: 1000 }, (_, i) => i); // Identity function const identity = (x) => x; // sum using reduce const sum = (arr) => arr.reduce((acc, curr) => acc + curr, 0); // sumBy using reduce with a function const sumBy = (fn, arr) => arr.reduce((acc, item) => acc + fn(item), 0);
Tests:
sum
sum(smallArray)
sumBy
sumBy(identity, smallArray)
lodash sum
_.sum(smallArray)
lodash sumBy
_.sumBy(identity, smallArray)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
sum
sumBy
lodash sum
lodash sumBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sum
2132441.8 Ops/sec
sumBy
2115608.0 Ops/sec
lodash sum
121888.3 Ops/sec
lodash sumBy
15658664.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of different methods for summing values in an array using both native JavaScript functions and the Lodash library. The benchmark specifically tests four implementations: 1. **Native JavaScript `sum`**: This method calculates the sum of array elements using the `reduce` function. 2. **Custom `sumBy` Function**: This implementation also utilizes the `reduce` function but allows accumulation based on a transformation function (in this case, the identity function). 3. **Lodash `_.sum`**: This function is part of the Lodash library, which provides utility functions for common programming tasks. Here, it sums the values in the array similarly to the native `sum` method. 4. **Lodash `_.sumBy`**: Similar to the custom `sumBy`, this method from Lodash sums the array values based on the transformation function provided. ### Comparison of Options 1. **Native `sum`**: - **Pros**: - Simple and efficient for summing values. - No external dependencies. - **Cons**: - May not be as expressive if transformations (like summing objects) are needed in future use cases. 2. **Custom `sumBy`**: - **Pros**: - Flexible: Allows summation based on any transformation/high-order function. - **Cons**: - Slightly more complex—requires understanding of higher-order functions and their performance implications. 3. **Lodash `_.sum`**: - **Pros**: - Highly optimized for performance and better readability. - Reduces boilerplate code, especially for common operations. - **Cons**: - Requires inclusion of a third-party library, which can increase bundle size and load times. 4. **Lodash `_.sumBy`**: - **Pros**: - Like `_.sum`, it leverages the optimizations in Lodash, along with transformation capabilities. - **Cons**: - Same dependency issue as `_.sum`, with additional overhead for creating the Lodash function. ### Benchmark Results The execution rates show that the `lodash sumBy` function performed the best, achieving around **15,658,664 executions per second**. The native `sum` implementation achieved **2,132,441.75 executions per second**, while `sumBy` and `lodash sum` performed noticeably lower, at approximately **2,115,608** and **121,888.32** executions per second, respectively. This indicates that the Lodash library's methods are optimized for performance, especially for frequent operations on large data sets. ### Other Considerations - **Performance Tuning**: Understanding which method performs best can inform decisions during development, especially when handling large datasets. - **Readability vs. Performance**: Using Lodash can lead to easier-to-read code but at the cost of additional dependency management. - **Future Scalability**: If your applications require frequent summations with transformations, relying on `sumBy` functions (either custom or from Lodash) may offer better scalability. ### Alternatives - **For Loops**: Traditional loops can be used for summation, and they may offer performance benefits since they avoid function calls overhead, although they sacrifice readability. - **Typed Arrays**: For numerical computations, JavaScript offers typed arrays that can significantly increase performance for operations on large datasets. - **Performance Libraries**: Consider other libraries, such as [RxJS](https://rxjs.dev/) or [D3.js](https://d3js.org/) for data manipulation if more complex data operations are required.
Related benchmarks:
Sum with for and reduce
forEach vs reduce vs map vs filter vs for vs lodash
lodash map vs filter vs reduce vs for each
filter map vs reduce
reduce vs for...of
Lodash Sum vs ApSum
Lodash.js vs Javascript (meanBy) 3.5
Lodash.js vs Javascript (meanBy) 3.6
meanBy vs native vs using sumBy 1.1
Comments
Confirm delete:
Do you really want to delete benchmark?