Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash sum vs reduce
(version: 1)
Comparing performance of:
sum vs reduce
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Tests:
sum
_.sum(arr)
reduce
arr.reduce((a, c) => a + c, 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sum
reduce
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
9823147.0 Ops/sec
reduce
54588488.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark being tested here compares two methods of summing an array of numbers in JavaScript: using the Lodash library's `_.sum` method and the native array method `reduce`. ### Benchmark Explanation 1. **Options Compared:** - `_.sum(arr)`: This method uses the Lodash library to compute the sum of elements in the given array `arr`. - `arr.reduce((a, c) => a + c, 0)`: This native JavaScript approach leverages the built-in `reduce` method of arrays to achieve the same result. ### Library Description - **Lodash**: Lodash is a modern JavaScript utility library that provides various functions to work with arrays, numbers, objects, strings, and more. It aims to simplify work with JavaScript by providing a lot of helpful utility functions and is known for its performance optimizations and consistency. ### Pros and Cons #### Using `_.sum(arr)` - **Pros:** - **Simplicity and Readability**: It is quite concise and easy to read, especially for developers familiar with Lodash. - **Consistency**: Lodash functions are highly optimized and ensure consistent behavior across various JavaScript environments. - **Bonus Features**: Potentially has built-in safeguards and checks, like handling edge cases, that might not be present in raw implementations. - **Cons:** - **Dependency**: Requires including the Lodash library, which increases the bundle size and can lead to longer loading times if not adequately managed. - **Performance Overhead**: Although optimized, there might be a slight overhead due to the additional abstraction. #### Using `arr.reduce((a, c) => a + c, 0)` - **Pros:** - **No Additional Dependencies**: Uses native JavaScript, meaning no external libraries are required, resulting in a smaller bundle size. - **Flexibility**: Allows customization, such as modifying the reducer function to handle different operations beyond summation. - **Cons:** - **Readability Mix**: For developers unfamiliar with functional programming paradigms, the syntax might not be as intuitive compared to using Lodash. - **Potential Performance Limitations**: If not optimized properly, using `reduce` could potentially be less efficient, though in many cases this is negligible due to JavaScript engine optimizations. ### Benchmark Results From the benchmark results, we see: - **Lodash Sum (`_.sum`) executions per second**: 11,819,274.0 - **Array Reduce executions per second**: 67,619,344.0 This indicates that using the native `reduce` method is significantly more efficient in this case, roughly 5.7 times faster than using `_.sum`. ### Other Alternatives Apart from the two methods covered in the benchmark, other alternatives to sum an array in JavaScript include: - **For Loop**: A traditional `for` loop can be used to iterate through the array and keep a running total. - **For Each Method**: Using `forEach` to iterate and sum values, although typically less efficient than `reduce`. - **Typed Arrays**: If performance is paramount and the values are numeric, utilizing typed arrays (e.g., `Int32Array`, `Float64Array`) can enhance performance, especially for large datasets. The choice between these methods often depends on the specific requirements of the project, including performance considerations, readability preference, and the existing codebase dependencies.
Related benchmarks:
lodash
Test: reduce vs for
Lodash sumBy string vs array vs function
lodash reduce vs native reduce
Map reduce vs sumBy
lodash sumBy vs normal sum of 4 values
lodash reduce vs array reduce
reduce vs _reduce
Lodash `sum()` vs `.reduce()`
Comments
Confirm delete:
Do you really want to delete benchmark?