Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs reduce
(version: 0)
Comparing performance of:
_.sumBy vs map and reduce
Created:
7 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>
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 100000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.sumBy
_.sumBy(arr,"value");
map and reduce
arr.reduce((prev, next) => prev + next.value, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sumBy
map and reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy
2369.7 Ops/sec
map and reduce
1811.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **Benchmark Overview** The MeasureThat.net benchmark compares the performance of two approaches: using Lodash's `sumBy` function versus using the built-in array `reduce()` method in JavaScript. The benchmark generates an array of 100,000 objects with random integer values and then tests both approaches to sum up all the values. **Options Compared** The benchmark compares two options: 1. **Lodash's `sumBy`**: This approach uses Lodash's `sumBy` function to iterate over the array and sum up the values. 2. **Built-in array `reduce()` method**: This approach uses the `reduce()` method to iterate over the array and sum up the values. **Pros and Cons** Here are some pros and cons of each approach: **Lodash's `sumBy`** Pros: * Easy to read and understand * Lodash provides a convenient way to perform common operations like summing up values Cons: * Adds an additional dependency on Lodash, which may slow down the benchmark due to the overhead of loading the library * May not be as efficient as native JavaScript code due to the added abstraction layer **Built-in array `reduce()` method** Pros: * Native JavaScript code means no dependencies or overhead * Can potentially be faster due to the absence of an abstraction layer Cons: * Requires more code and is less readable than Lodash's `sumBy` * May not be as convenient for complex operations like summing up values with different functions **Other Considerations** The benchmark also takes into account the following factors: * **Hardware**: The test is run on a desktop computer with Chrome 129, which suggests that the results may not be representative of mobile devices or other platforms. * **Browser engine**: The use of Safari as the browser engine (alongside Chrome) may affect the results due to differences in rendering and JavaScript execution. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of functional programming helpers. In this case, `sumBy` is used to sum up the values in an array while applying a given function to each element. The library provides several other useful functions for data manipulation, such as `filter()`, `map()`, and `compact()`. **Special JavaScript Feature or Syntax** There are no special JavaScript features or syntaxes mentioned in this benchmark. However, it's worth noting that the use of Lodash may be considered a "feature" in some cases, but in this context, it's simply used as a convenient dependency to simplify the code. **Alternatives** If you want to run a similar benchmark on your own, you can try using other libraries or approaches, such as: * Using only native JavaScript methods like `forEach()` and `Array.prototype.reduce()` * Implementing your own custom summing function * Using a different library like `lodash` with a different version (e.g., `4.17.2`)
Related benchmarks:
Lodash sumBy vs simple reduce
Lodash sumBy (value and map) vs reduce
[2024] Lodash .sumBy vs. Reduce with / 10002
Lodash sumBy vs reduce v0.0.1
Comments
Confirm delete:
Do you really want to delete benchmark?