Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash maxBy vs Math.max & Map
(version: 0)
Comparing performance of:
_.maxBy vs Math.max and map
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.0.1/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 < 1000; i++) { arr.push({ value: getRandomInt(100) }); } function getValue(ob) { return ob.value; }
Tests:
_.maxBy
_.maxBy(arr, (o)=>getValue(o));
Math.max and map
Math.max(arr.map(o => getValue(o)))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.maxBy
Math.max and map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.maxBy
625977.9 Ops/sec
Math.max and map
107517.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark JSON. **Benchmark Overview** The benchmark is designed to compare two approaches for finding the maximum value in an array: 1. Using Lodash's `maxBy` function, which finds the element with the maximum value in the given array based on a custom comparison function. 2. Using JavaScript's built-in `Math.max` function with an arrow function that extracts the `value` property from each object in the array. **Options Compared** The benchmark is comparing two options: 1. **Lodash's `maxBy`**: This function uses the `_.maxBy` method, which takes two arguments: the array to search for the maximum value and a comparison function. 2. **JavaScript's built-in `Math.max` with map**: This approach first maps each object in the array to its `value` property using an arrow function, and then finds the maximum value among these mapped values using `Math.max`. **Pros and Cons** Here are some pros and cons of each approach: 1. **Lodash's `maxBy`**: * Pros: + More readable code when dealing with complex comparison functions. + Lodash provides a convenient way to find the maximum value in an array without having to write custom logic. * Cons: + Requires including Lodash as a dependency, which may add overhead. + May be slower due to the additional function call and iteration over the array. 2. **JavaScript's built-in `Math.max` with map**: * Pros: + Faster execution since it only requires two iterations: mapping and finding the maximum value. + No dependency on Lodash, reducing overhead. * Cons: + Code may be less readable due to the use of arrow functions and `map`. + May require additional logic for handling cases where the array is empty or null. **Library Used** In this benchmark, Lodash is used as a library. The `getValue` function is extracted from the script preparation code and passed as an argument to `_.maxBy`. Lodash's purpose here is to provide a convenient way to find the maximum value in the array based on a custom comparison function. **Special JS Feature** There are no special JavaScript features or syntax used in this benchmark. It only relies on standard JavaScript concepts, such as arrow functions and array methods. **Other Alternatives** If you were to implement a custom solution for finding the maximum value in an array, some alternatives could include: 1. Using a simple loop to iterate over the array and keep track of the maximum value. 2. Using a more efficient algorithm, such as the Quicksort or Merge Sort algorithms. However, these approaches would likely be less readable and maintainable than using Lodash's `maxBy` function or JavaScript's built-in `Math.max` with map. In summary, this benchmark provides a useful comparison between two common approaches for finding the maximum value in an array: using Lodash's `maxBy` function versus JavaScript's built-in `Math.max` with map. The choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
Lodash max vs Math.max (lodash 4.7.11)
Labels
Lodash orderBy() vs array.prototype.sort
Lodash max vs JS Math.max (2022)
Comments
Confirm delete:
Do you really want to delete benchmark?