Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sum vs Reduce for ten thousand elements
(version: 0)
Comparing performance of:
lodash sum vs javascript reduce
Created:
5 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>
Tests:
lodash sum
const numbersFromOneUpToTenThousand = [...Array(10000).keys()]; console.log( _.sum(numbersFromOneUpToTenThousand) );
javascript reduce
const numbersFromOneUpToTenThousand = [...Array(10000).keys()]; console.log( numbersFromOneUpToTenThousand.reduce((a,b) => a+b) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash sum
javascript reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash sum
4860.6 Ops/sec
javascript reduce
5355.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and other considerations. **Benchmark Overview** MeasureThat.net is a platform where users can create and run JavaScript microbenchmarks. The benchmark in question compares two ways to sum up an array of 10,000 elements: using Lodash's `sum` function and the built-in `reduce` method. **Test Cases** There are two test cases: 1. **Lodash Sum**: This test case uses Lodash's `sum` function to calculate the sum of the array. 2. **JavaScript Reduce**: This test case uses the built-in `reduce` method to calculate the sum of the array. **Options Compared** The main difference between these two approaches is how they handle accumulation and iteration: * **Lodash Sum**: Uses Lodash's implementation, which takes care of accumulating the sum. This approach is likely faster because it leverages optimized internal logic. * **JavaScript Reduce**: Manually accumulates the sum using a callback function (in this case, `a + b`). While this approach can be more straightforward, it may incur additional overhead due to the manual iteration. **Pros and Cons** Here are some pros and cons of each approach: * **Lodash Sum**: + Pros: Faster execution, optimized internal logic + Cons: Requires external library (Lodash), may not provide a pure JavaScript experience * **JavaScript Reduce**: + Pros: Pure JavaScript implementation, potentially lower overhead for smaller arrays + Cons: May incur additional overhead due to manual iteration and accumulation **Library and Syntax** The Lodash library is used in the `lodash.sum` test case. Lodash is a popular utility library that provides various mathematical functions, among others. There are no special JavaScript features or syntaxes being tested in this benchmark. However, it's worth noting that using external libraries like Lodash can provide additional functionality and convenience, but may also introduce overhead due to the need to load an extra file. **Other Alternatives** If you're interested in testing alternative approaches, here are some options: * **NumJS**: A JavaScript library for numerical computations, which provides a `sum` function similar to Lodash. * **Built-in Array.prototype.reduce()**: You could test using only the built-in `reduce()` method without any additional libraries. * **Other implementations**: There may be other optimized implementations of `sum` or `reduce` available in specific JavaScript engines or libraries. Keep in mind that these alternatives might not be as performant or efficient as Lodash's implementation, but they can provide a pure JavaScript experience and potentially lower overhead for smaller arrays.
Related benchmarks:
Lodash reduce with native reduce
Lodash reduce vs native
lodash reduce vs native reduce
Lodash reduce vs native (testing)
lodash reduce vs array reduce
Comments
Confirm delete:
Do you really want to delete benchmark?