Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash min & max vs math.min & math.max vs for loop
(version: 0)
Comparing performance of:
_.max vs Math.max vs for loop
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.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 < 2000; i++){ arr.push({value:getRandomInt(5000)}); }
Tests:
_.max
_.max(arr); _.min(arr);
Math.max
Math.max(...arr) Math.min(...arr)
for loop
let min = 0; let max = 0; for (let index = 0; index < arr.length; index++) { const element = arr[index]; if (arr[index] < min) { min = arr[index]; } if (arr[index] > max) { max = arr[index]; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.max
Math.max
for loop
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 world of JavaScript microbenchmarks on MeasureThat.net. **Overview** The provided benchmark is designed to compare the performance of three approaches for finding the minimum and maximum values in an array: 1. Lodash `max` and `min` functions 2. Built-in `Math.max` and `Math.min` functions 3. A manual implementation using a for loop **Library: Lodash** The benchmark uses the popular JavaScript library, Lodash (version 3.10.1), which provides a set of functional programming helpers. The `_.max` function takes an array as input and returns the maximum value. **Manual Implementation** The third test case uses a simple for loop to find the minimum and maximum values in the array. This approach requires manual iteration through each element, making it less efficient compared to the other two options. **Comparison of Options** Here's a brief overview of each option's pros and cons: 1. **Lodash `max` and `min` functions** * Pros: Portable, concise, and often faster than built-in functions due to their optimized implementation. * Cons: Requires Lodash library to be included, which may add overhead depending on the specific use case. 2. **Built-in `Math.max` and `Math.min` functions** * Pros: Built into most JavaScript engines, no additional dependencies required. * Cons: May have performance issues for large arrays due to array iteration or caching. 3. **Manual Implementation (for loop)** * Pros: Control over the implementation, often faster for small arrays or specific use cases. * Cons: Requires manual iteration through each element, can be error-prone and less efficient. **Browser-Specific Results** The latest benchmark results show that: 1. Lodash `max` function is slightly faster than the built-in `Math.max` function, with an execution rate of around 2584.669677734375 executions per second. 2. Built-in `Math.max` function has a lower execution rate, around 1856.923095703125 executions per second. 3. The manual for loop implementation is the fastest, with an execution rate of around 5455.37109375 executions per second. Keep in mind that these results are browser-specific and may vary depending on the specific JavaScript engine, hardware, and other factors. **Other Alternatives** If you're looking for alternative libraries or approaches, consider: 1. **Array.prototype.reduce**: Some browsers have optimized implementations of `reduce` which can be used for similar operations. 2. **ES6 Array.prototype.map()`: While not directly comparable to `max()` or `min()`, `map()` can be used as a starting point for more complex operations. 3. **Other libraries**: There are other libraries, like Ramda or Liskov's `minMax`, that provide similar functionality. Please note that the performance differences between these alternatives may vary depending on your specific use case and requirements.
Related benchmarks:
Lodash max vs Math.max (lodash 4.7.11)
Lodash min vs Math.min (lodash 4.7.11)
Labels
Lodash max vs JS Math.max (2022)
Lodash.isArray vs Array.isArray (Lodash v4.17.15)
Comments
Confirm delete:
Do you really want to delete benchmark?