Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash 4.17.21 sumBy vs reduce
(version: 0)
Comparing performance of:
_.sumBy vs map and reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></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:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sumBy
2464.6 Ops/sec
map and reduce
1712.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare two approaches: using Lodash's `sumBy` function and using JavaScript's built-in `reduce` method. The test creates an array of objects with random values and calculates the sum of all values in two different ways. **Options being Compared** 1. **Lodash's `sumBy`**: This is a utility function that takes an array, a key path to access each element (in this case, `"value"`), and a callback function to apply to each element. 2. **JavaScript's built-in `reduce` method**: This is a method that applies a callback function to each element in the array, reducing it to a single value. **Pros and Cons of Each Approach** 1. **Lodash's `sumBy`**: * Pros: Easy to use, concise code, optimized for performance. * Cons: Requires Lodash library to be included in the test environment. 2. **JavaScript's built-in `reduce` method**: * Pros: Native implementation, no additional libraries required. * Cons: More verbose code, requires manual handling of initial value and callback function. **Library Used** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. In this benchmark, Lodash's `sumBy` function is used to calculate the sum of all values in the array. **Special JS Feature/Syntax** There is no specific special feature or syntax being tested in this benchmark. It only deals with standard JavaScript functions and libraries. **Other Alternatives** If you wanted to test alternative approaches, here are a few examples: 1. **Using `forEach`**: You could use the `forEach` method to iterate over the array and calculate the sum manually. 2. **Using `every` and `some`**: You could use the `every` and `some` methods to check if all elements in the array meet a certain condition (e.g., are greater than 0) before calculating the sum. 3. **Using a custom implementation with `for` loops**: You could implement your own loop-based solution using traditional JavaScript loops. Keep in mind that these alternatives would likely have different performance characteristics and code complexity compared to the Lodash `sumBy` function or JavaScript's built-in `reduce` method.
Related benchmarks:
Lodash sumBy vs reduce
Lodash sumBy vs simple 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?