Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash min & max vs math.min & math.max vs for loop (positive & negative float)
(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) { num = Math.random() * 10000; num *= Math.round(Math.random()) ? 1 : -1; return num; } 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 break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares three approaches to find the minimum and maximum values in an array: 1. `_.max` (using Lodash library) 2. `Math.max` (built-in JavaScript function) 3. A custom for loop approach **Lodash (`_.max`)** * Library: Lodash is a popular utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. * Purpose: The `_.max` function returns the largest value in an array. In this benchmark, `_.max` is used to find the maximum value in the generated array. The test measures how long it takes to execute this function. **Built-in JavaScript (`Math.max`)** * Syntax: `Math.max(...arr)` (using rest parameter syntax) * Purpose: The `Math.max` function returns the largest value of a set of arguments provided as an argument. In this benchmark, `Math.max` is used twice: once to find the maximum value in the array and again to compare it with another value. The test measures how long it takes to execute these two calls. **Custom For Loop Approach** * Syntax: ```javascript 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]; } } ``` * Purpose: This approach iterates through the array and keeps track of the minimum and maximum values found so far. The test measures how long it takes to execute this custom for loop approach. **Other Considerations** * The benchmark uses a large array of 2000 elements, each with a random integer value between -5000 and 5000. This generates a large dataset that can be used to measure performance. * The test is run on a Chrome browser (version 90) on a Mac OS X 10.14.0 platform. **Alternatives** If you were to rewrite this benchmark, you could consider the following alternatives: * Using `Array.prototype.reduce` instead of Lodash's `_max` * Using `Math.min` and `Math.max` with an initial value of `-Infinity` and `Infinity` respectively * Using a different data structure, such as a binary search tree or a heap, to find the minimum and maximum values in the array However, keep in mind that these alternatives may not provide accurate results if they are not optimized for performance. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * Lodash (`_.max`): + Pros: Easy to use, well-maintained library + Cons: Adds overhead due to function call and memory allocation * Built-in JavaScript (`Math.max`): + Pros: Fast, lightweight, and optimized for performance + Cons: Limited functionality compared to Lodash's `_max` * Custom For Loop Approach: + Pros: Allows for fine-grained control over iteration and data access + Cons: Can be slower due to the overhead of manual iteration and comparison
Related benchmarks:
Lodash max vs Math.max (lodash 4.7.11)
Lodash min & max vs math.min & math.max vs for loop
Lodash vs Math vs for loop (positive & negative float)
Lodash max vs JS Math.max (2022)
Comments
Confirm delete:
Do you really want to delete benchmark?