Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sumBy vs reduce 1000000 elements
(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.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 < 1000000; 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:
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 dive into the benchmark definition and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: 1. `_.sumBy(arr,"value")`: This is a JavaScript function from the Lodash library that calculates the sum of values in an array using a specified key. 2. `arr.reduce((prev, next) => prev + next.value, 0)`: This is a built-in JavaScript method that reduces an array to a single value by iterating through it and accumulating a result. Both approaches are being tested on a large array of 1 million elements, where each element has a random "value" property. **Options Compared** The two options being compared are: * Using the `_.sumBy` function from Lodash library * Implementing the reduction using the built-in `reduce()` method **Pros and Cons** Using `_.sumBy` from Lodash library: Pros: * Concise and readable code * Handles edge cases, such as null or undefined values * Well-tested and maintained by the Lodash community Cons: * External dependency on the Lodash library (adds overhead) * May have performance impact due to function call overhead Implementing `reduce()` method: Pros: * No external dependencies * Can be optimized for performance with custom implementations * Built-in functionality, so no additional memory is required Cons: * Requires more code and may be less readable * May not handle edge cases as elegantly as `_.sumBy` **Other Considerations** The benchmark also considers the overhead of executing JavaScript on a Linux desktop environment using Chrome 111. **Test Case Library** In this case, the test case library used is Lodash. Lodash is a popular JavaScript utility library that provides a lot of useful functions for tasks such as string manipulation, array manipulation, and more. The `_.sumBy` function is one of its many utility functions that can be used to perform common operations. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. It's a straightforward comparison of two implementation approaches. **Alternative Approaches** Other alternative approaches could include: * Using `Array.prototype.reduce()` method with a custom callback function * Implementing the reduction using a loop and accumulator variables * Using a third-party library that provides similar functionality to Lodash (e.g., Ramda) * Using a JavaScript engine-specific optimization (e.g., V8's built-in optimizations) However, it's worth noting that the benchmark is designed to compare two specific implementation approaches, so exploring alternative approaches may not add much value to the comparison.
Related benchmarks:
Lodash sumBy vs reduce
Lodash sumBy vs simple reduce
Lodash sumBy vs forEach vs for
Lodash sumBy (value and map) vs reduce
Lodash sumBy vs reduce v0.0.1
Comments
Confirm delete:
Do you really want to delete benchmark?