Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy (value and map) vs reduce
(version: 0)
Comparing performance of:
_.sumBy value vs map and reduce vs _.sumBy with map
Created:
2 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 value
_.sumBy(arr, "value");
map and reduce
arr.reduce((prev, next) => prev + next.value, 0);
_.sumBy with map
_.sumBy(arr, x => x.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.sumBy value
map and reduce
_.sumBy with map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPad; CPU OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 131 on iOS 17.6.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy value
3703.6 Ops/sec
map and reduce
5794.2 Ops/sec
_.sumBy with map
5630.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark is designed to compare two approaches for summing values in an array: Lodash's `sumBy` function with a callback, and the built-in `reduce` method of JavaScript arrays. The benchmark aims to determine which approach is faster for large datasets. **Script Preparation Code** This code generates an array of 100,000 objects, each containing a random integer value between 0 and 100. This data structure is used as input for both the Lodash `sumBy` function and the built-in `reduce` method. **Html Preparation Code** This code includes the necessary JavaScript library for Lodash (version 4.17.5) to be executed alongside the benchmark script. The library provides a utility function `sumBy`, which is used in one of the test cases. **Individual Test Cases** There are three test cases: 1. `_.sumBy arr, "value"`: This test case uses Lodash's `sumBy` function with a string callback to sum up the values in the array. 2. `arr.reduce((prev, next) => prev + next.value, 0)` : This test case uses the built-in `reduce` method of JavaScript arrays to sum up the values. 3. `_.sumBy arr, x => x.value`: This test case uses Lodash's `sumBy` function with an arrow function callback to sum up the values in the array. **Options Compared** The benchmark compares two options: * **Lodash `sumBy` function**: This is a utility function provided by the Lodash library that sums up values in an array using a custom callback. * **Built-in JavaScript `reduce` method**: This is a built-in method of JavaScript arrays that can be used to reduce an array to a single value. **Pros and Cons** Here are some pros and cons for each option: * **Lodash `sumBy` function**: + Pros: Easy to use, concise syntax. + Cons: Requires additional library dependency (Lodash), may have overhead due to the callback function. * **Built-in JavaScript `reduce` method**: + Pros: Built-in, no extra dependencies, can be customized through callback function. + Cons: May require more code, can be less readable for complex transformations. **Library and Its Purpose** The Lodash library is a popular utility library for JavaScript that provides a collection of functional programming helpers. In this benchmark, the `sumBy` function is used to sum up values in an array using a custom callback. **Special JS Feature or Syntax** There are no special features or syntaxes used in this benchmark. The code focuses on demonstrating two simple algorithms: Lodash's `sumBy` function and the built-in JavaScript `reduce` method. **Other Alternatives** If you're looking for alternative approaches to summing values in an array, here are a few options: * Using a custom loop with indexing * Utilizing other utility libraries (e.g., Underscore.js) * Leveraging WebAssembly or native array methods (if supported by the browser) Keep in mind that these alternatives may have different performance characteristics, readability, and usage patterns compared to Lodash's `sumBy` function and the built-in JavaScript `reduce` method.
Related benchmarks:
Lodash sumBy vs map and reduce
Lodash sumBy vs reduce
Lodash sumBy vs simple reduce
Lodash sumBy vs forEach vs for vs map reduce
Comments
Confirm delete:
Do you really want to delete benchmark?