Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash `sum()` vs `.reduce()`
(version: 0)
Comparing performance of:
Custom vs Lodash
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
toSum = (arr) => arr.reduce((prev, curr) => prev + curr, 0);
Tests:
Custom
toSum([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
Lodash
_.sum([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Custom
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 OPR/119.0.0.0
Browser/OS:
Opera 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Custom
46222504.0 Ops/sec
Lodash
9027102.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches to calculate the sum of an array: using the `reduce()` method and using a custom implementation. The test also includes another variant that uses the Lodash library's `sum()` function. **Comparison Options** 1. **Custom Implementation**: This option uses a simple loop to iterate over the array elements and add them up. 2. **Lodash `sum()`**: This option uses the Lodash library's built-in `sum()` function, which is optimized for performance. 3. **Lodash `.reduce()`**: This option uses the Lodash library's `reduce()` function, which is also optimized for performance. **Pros and Cons of Each Approach** 1. **Custom Implementation**: * Pros: No external dependency, easy to understand and implement. * Cons: May not be optimized for performance, can be slower than other options. 2. **Lodash `sum()`**: * Pros: Optimized for performance, provides a simple and efficient way to calculate the sum. * Cons: Requires including an additional library (Lodash), may not be as easy to understand for those without knowledge of Lodash. 3. **Lodash `.reduce()`**: * Pros: Provides a flexible and powerful way to calculate the sum, optimized for performance. * Cons: Requires including an additional library (Lodash), may require more understanding of Lodash's API. **Library Used** The test uses the Lodash library version 4.17.10. Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string manipulation, and more. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. The tests only use standard JavaScript functionality. **Other Alternatives** If you want to explore alternative approaches, here are a few options: * Using `Array.prototype.reduce()` without Lodash: This approach is similar to the custom implementation but uses the built-in `reduce()` method. * Using `Array.prototype.forEach()` and a variable to accumulate the sum: This approach is another way to iterate over an array and calculate the sum. In conclusion, this benchmark provides a simple and informative comparison of three approaches to calculate the sum of an array: custom implementation, Lodash `sum()`, and Lodash `.reduce()`. The results can help developers choose the most efficient and suitable approach for their specific use cases.
Related benchmarks:
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?