Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy with lambda vs Array reduce
(version: 0)
Comparing performance of:
_.sumBy vs map and reduce
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>
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,x => x.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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy
1139.1 Ops/sec
map and reduce
1740.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Overview** This benchmark compares two approaches to calculate the sum of values in an array: `_.sumBy` from Lodash and the `Array.prototype.reduce()` method. We'll break down each test case, explain the options being compared, their pros and cons, and discuss any special JavaScript features or syntax used. **Test Case 1: _.sumBy** The benchmark definition is: ``` _.sumBy(arr,x => x.value); ``` This uses the `_.sumBy()` function from Lodash, which takes an array (`arr`) and a callback function (`x => x.value`). The callback function extracts the value from each object in the array (`x.value`). **Options being compared** In this test case, only one option is being compared: * `_.sumBy(arr,x => x.value)` Pros of using `_._sumBy()`: * Concise and expressive code * Avoids manual iteration over the array Cons: * Depends on Lodash library being loaded **Alternative approach** The alternative approach used in the benchmark is: ``` arr.reduce((prev, next) => prev + next.value, 0); ``` This uses the `Array.prototype.reduce()` method to iterate over the array and sum up the values. Pros of using `reduce()`: * Built-in method in JavaScript * No external library dependency Cons: * Requires manual iteration over the array (in this case, using a callback function) **Test Case 2: map and reduce** The benchmark definition is: ``` arr.reduce((prev, next) => prev + next.value, 0); ``` This uses the `Array.prototype.reduce()` method to iterate over the array and sum up the values. **Options being compared** In this test case, two options are being compared: * `arr.reduce((prev, next) => prev + next.value, 0)` * `_.sumBy(arr,x => x.value)` (which is not present in the provided benchmark result) Pros of using `reduce()`: * Built-in method in JavaScript * No external library dependency Cons: * Requires manual iteration over the array (in this case, using a callback function) **Library: Lodash** Lodash is a utility library that provides various functions for functional programming, data manipulation, and more. In this benchmark, `_.sumBy()` is used to calculate the sum of values in an array. **DevicePlatform: Desktop** The benchmark was run on a desktop device (Linux). **Other considerations** * The benchmark uses a large array (`arr`) with 100,000 elements, which may lead to performance differences between the two approaches. * The `getRandomInt()` function is used to populate the array with random values, which can affect the performance of the benchmark. **Alternatives** If you're looking for alternative libraries or methods to calculate the sum of values in an array, here are a few options: * `Array.prototype.forEach()`: This method allows you to iterate over the array and perform an action on each element. You could use it with a callback function to calculate the sum. * `forEach` with `map` and `reduce`: Another approach is to use `forEach()` to iterate over the array, then use `map()` to extract the values, and finally use `reduce()` to calculate the sum. * External libraries like `Ramda` or `PicoReduce` provide similar functionality to Lodash's `_.sumBy()`.
Related benchmarks:
Lodash sumBy vs reduce
Lodash sumBy vs simple reduce
Lodash sumBy vs forEach vs for
Lodash sumBy vs forEach vs for vs map reduce
Comments
Confirm delete:
Do you really want to delete benchmark?