Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash max vs Math.max (lodash 4.7.11)
(version: 0)
Comparing performance of:
_.max vs Math.max
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.7.11/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)}); }
Tests:
_.max
_.max(arr);
Math.max
Math.max(...arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.max
Math.max
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.max
14253.7 Ops/sec
Math.max
16992.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and explanations. **Benchmark Definition:** The benchmark compares two approaches for finding the maximum value in an array: 1. `_.max(arr)` using Lodash library (version 4.7.11) 2. `Math.max(...arr)` (using the built-in JavaScript function) **Options Compared:** * **Lodash's `_`-prefixed functions**: These are utility functions provided by the Lodash library, which extends JavaScript with additional features. * **Built-in JavaScript functions**: Specifically, the `max()` function within the `Math` object. **Pros and Cons of Each Approach:** 1. **Lodash's `_._max`**: * Pros: Often provides more functionality and flexibility, especially for complex data structures like objects or arrays with nested properties. * Cons: Adds an additional library dependency, which may not be ideal for smaller projects or those targeting older browsers due to polyfill requirements. 2. **Built-in JavaScript `Math.max()`**: * Pros: No additional dependencies required, and it's a well-established standard function in the JavaScript ecosystem. * Cons: May not support all edge cases or data structures (e.g., objects with nested properties). **Lodash Library Explanation:** The Lodash library is a popular utility belt for JavaScript that provides a comprehensive set of functional programming helpers. The `_max` function within Lodash finds the maximum value in an array, handling various types of input and edge cases. **JavaScript Feature/ Syntax Explanation (Not applicable):** Since neither of the tested approaches relies on a special JavaScript feature or syntax, there's nothing to explain here. **Other Alternatives:** If you need to find the maximum value in an array without using Lodash or the built-in `Math.max()` function: * **Array.prototype.reduce()**: You can use this method to reduce the array to a single value (the maximum) by providing a callback function that takes two arguments, `a` and `b`, where `a` is the accumulator and `b` is the current element being processed. * **Array.prototype.forEach()**: Although not as efficient as `reduce()` or `Math.max()`, you can use this method to iterate through the array and accumulate the maximum value. Here's a basic example using `reduce()`: ```javascript const arr = [...]; // your array let max = arr[0]; arr.forEach((element) => { if (element > max) { max = element; } }); ``` Keep in mind that these alternatives might be less efficient than the built-in `Math.max()` function.
Related benchmarks:
Lodash min vs Math.min (lodash 4.7.11)
Labels
Lodash max vs JS Math.max (2022)
_.max vs Math.max
Comments
Confirm delete:
Do you really want to delete benchmark?