Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sum vs Reduce vs forOf
(version: 0)
Comparing performance of:
lodash sum vs javascript reduce vs for of
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.5/lodash.min.js'></script>
Tests:
lodash sum
const array = [4381, 1252, 35, 3456, 2726, 5553, 428, 2780, 4858, 8675, 446, 1426, 2864, 2235, 3320, 1561, 876, 8846, 122, 8076, 3440, 7604, 6486, 6725, 9840, 4568, 7817, 6310, 8477, 6835, 4011, 4762, 2358, 7583, 2376, 80, 5057, 8005, 8921, 3547, 7172, 4627, 698, 1796, 9368, 4778, 8412, 9195, 1278, 7992, 2091, 8025, 8586, 8344, 8380, 9398, 8652, 8613, 2789, 1044, 2711, 6886, 2021, 4682, 5356, 4944, 9344, 3863, 8705, 9233, 6525, 4933, 6252, 4408, 2885, 9206, 900, 8021, 7225, 866, 3048, 2005, 4913, 2632, 2595, 882, 4697, 7561, 7455, 2, 8567, 9786, 845, 7502, 7343, 7077, 5132, 5177, 4042, 1608]; _.sum(array)
javascript reduce
const array = [4381, 1252, 35, 3456, 2726, 5553, 428, 2780, 4858, 8675, 446, 1426, 2864, 2235, 3320, 1561, 876, 8846, 122, 8076, 3440, 7604, 6486, 6725, 9840, 4568, 7817, 6310, 8477, 6835, 4011, 4762, 2358, 7583, 2376, 80, 5057, 8005, 8921, 3547, 7172, 4627, 698, 1796, 9368, 4778, 8412, 9195, 1278, 7992, 2091, 8025, 8586, 8344, 8380, 9398, 8652, 8613, 2789, 1044, 2711, 6886, 2021, 4682, 5356, 4944, 9344, 3863, 8705, 9233, 6525, 4933, 6252, 4408, 2885, 9206, 900, 8021, 7225, 866, 3048, 2005, 4913, 2632, 2595, 882, 4697, 7561, 7455, 2, 8567, 9786, 845, 7502, 7343, 7077, 5132, 5177, 4042, 1608]; array.reduce((a,b) => a+b)
for of
const array = [4381, 1252, 35, 3456, 2726, 5553, 428, 2780, 4858, 8675, 446, 1426, 2864, 2235, 3320, 1561, 876, 8846, 122, 8076, 3440, 7604, 6486, 6725, 9840, 4568, 7817, 6310, 8477, 6835, 4011, 4762, 2358, 7583, 2376, 80, 5057, 8005, 8921, 3547, 7172, 4627, 698, 1796, 9368, 4778, 8412, 9195, 1278, 7992, 2091, 8025, 8586, 8344, 8380, 9398, 8652, 8613, 2789, 1044, 2711, 6886, 2021, 4682, 5356, 4944, 9344, 3863, 8705, 9233, 6525, 4933, 6252, 4408, 2885, 9206, 900, 8021, 7225, 866, 3048, 2005, 4913, 2632, 2595, 882, 4697, 7561, 7455, 2, 8567, 9786, 845, 7502, 7343, 7077, 5132, 5177, 4042, 1608]; let result = array[0]; for(let i = 1; i < array.length; i++) { result += array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash sum
javascript reduce
for of
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash sum
3068419.0 Ops/sec
javascript reduce
26107918.0 Ops/sec
for of
24682736.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript algorithms is essential for optimizing code and identifying potential bottlenecks. **Benchmark Definition JSON** The benchmark definition defines three test cases: 1. `Sum vs Reduce vs forOf`: This benchmark compares the execution times of three different approaches to calculate the sum of an array: * Using the `_.sum()` method from the Lodash library. * Using the `reduce()` method provided by JavaScript. * Using a simple `for` loop to iterate through the array and add up its elements. 2. `lodash sum`, `javascript reduce`, and `for of`: These are the actual test names for each of the three approaches defined above. **Options Compared** The benchmark compares the following options: 1. **Lodash Sum**: This option uses the `_.sum()` method from the Lodash library to calculate the sum of the array. 2. **JavaScript Reduce**: This option uses the built-in `reduce()` method provided by JavaScript to calculate the sum of the array. 3. **For Of Loop**: This option uses a simple `for` loop to iterate through the array and add up its elements. **Latest Benchmark Results** The latest benchmark results show that: 1. The Lodash Sum approach performs the worst, with an average execution time of 3068419 seconds per second (sps). 2. The JavaScript Reduce approach performs better than Lodash Sum, but still has a relatively high execution time of 24682736 sps. 3. The For Of Loop approach performs the best, with an average execution time of 26107918 sps. **Conclusion** Based on these results, it appears that the `for` loop approach is the fastest way to calculate the sum of an array in JavaScript, followed closely by the built-in `reduce()` method. The Lodash Sum approach, while convenient and easy to use, is significantly slower than the other two options.
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?