Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs simple reduce
(version: 0)
Comparing performance of:
_.sumBy vs reduce
Created:
5 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");
reduce
arr.reduce((prev, next) => prev.value + next.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.sumBy
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Purpose** The benchmark measures the performance difference between two approaches to calculate the sum of values in an array: 1. Using `lodash.sumBy` (a library function) for JavaScript arrays. 2. Using the built-in `Array.prototype.reduce()` method without any libraries or additional functions. **Options Compared** Two options are compared: A) Lodash's `sumBy` function B) The built-in `reduce()` method **Pros and Cons of Each Approach** ### Lodash's `sumBy` **Pros:** 1. **Easy to use**: `sumBy` is a simple, concise, and well-documented function. 2. **Optimized for performance**: Lodash is a popular and widely-used library that has optimized its functions for performance. **Cons:** 1. **Added dependency**: Using Lodash requires including an additional JavaScript file (lodash.min.js) in your code, which may not be desirable in some cases. 2. **Overhead of function call**: Although Lodash's `sumBy` is optimized, it still incurs a function call overhead. ### Built-in `reduce()` **Pros:** 1. **No dependencies or overhead**: Using the built-in `reduce()` method doesn't require any additional libraries or functions. 2. **Familiar syntax**: Developers already familiar with JavaScript may find this approach more intuitive and easier to understand. **Cons:** 1. **More verbose**: The code for using `reduce()` can be more verbose than Lodash's `sumBy`. 2. **Potential performance overhead**: Although the built-in `reduce()` method is optimized, it may still incur some overhead due to its native JavaScript implementation. **Library and Purpose (Lodash)** The Lodash library is a popular JavaScript utility library that provides a wide range of functions for common tasks, such as array manipulation, string manipulation, and more. The `sumBy` function in particular is designed to calculate the sum of values in an array based on a provided callback function. **Special JS Feature or Syntax** None are mentioned in this specific benchmark, but it's worth noting that some JavaScript features, like async/await or destructuring, may be used in other benchmarks.
Related benchmarks:
Lodash sumBy vs 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?