Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash maxBy vs reduce
(version: 0)
Comparing performance of:
_.maxBy vs 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 < 100000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.maxBy
_.maxBy(arr,"value");
reduce
arr.reduce((prev, next) => next.value > prev.value ? next : prev);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.maxBy
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.maxBy
1322.0 Ops/sec
reduce
1547.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark data and explain what's being tested, compared, and other considerations. **Benchmark Definition** The benchmark is comparing two approaches to find the maximum element in an array: 1. `_.maxBy(arr, "value")`: This uses the Lodash library (`_`) to find the element with the maximum value in the array. The second argument `"value"` specifies that we want to compare the elements based on their `value` property. 2. `arr.reduce((prev, next) => next.value > prev.value ? next : prev)`: This is a native JavaScript approach using the `reduce()` method to iterate over the array and find the maximum element. **Options Compared** The two approaches are being compared in terms of performance (measured by executions per second). The Lodash library's `maxBy()` function is optimized for performance, while the native `reduce()` method can be slower due to its iterative nature. **Pros and Cons** * **Lodash's `maxBy()`**: Pros: + Optimized for performance + Easy to use with a simple API * Cons: + Requires an additional library download (lodash) + May not be as familiar to developers without experience with Lodash * **Native `reduce()` method**: + Pros: - No additional library required - Familiar to most JavaScript developers + Cons: - Can be slower due to its iterative nature - Requires more code and a better understanding of the `reduce()` method **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string manipulation, and functional programming. In this case, `maxBy` is used to find the element with the maximum value in an array. **Other Considerations** * **Data Generation**: The benchmark generates an array of 100,000 random objects with a `value` property using the `getRandomInt()` function. This ensures that both approaches are tested on a large dataset. * **Test Environment**: The test is run on a Chrome browser on Linux desktop platforms. **Alternatives** If you're interested in exploring alternative approaches or libraries for this specific use case, here are some options: 1. Using `Math.max()` with the spread operator (`...`): `Math.max(...arr.map(obj => obj.value))` 2. Using `Array.prototype.map()` and `Array.prototype.reduce()`: `arr.map(obj => obj.value).reduce((max, current) => max > current ? max : current)` 3. Using a library like jQuery (for older browsers that don't support Lodash or native JavaScript methods) These alternatives might not be as efficient as the original approaches but can provide a different perspective on solving the problem.
Related benchmarks:
Lodash max vs Math.max (lodash 4.7.11)
Labels
Lodash sort vs array.prototype.sort - 2
Lodash max vs JS Math.max (2022)
Comments
Confirm delete:
Do you really want to delete benchmark?