Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max vs. custom max
(version: 0)
N/A
Comparing performance of:
Math.max vs Custom
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let lowerBound = -Number.MAX_VALUE; function max() { let maximum = lowerBound; let i = arguments.length; while (i--) { arguments[i] > maximum && (maximum = arguments[i]) } return maximum; };
Tests:
Math.max
return Math.max(1,21,53,12,58,65,32,12,445,9382,132,12398,1239812, 21938721, 981, 91283, 1, 1, 1, 1, 1, 102183129312)
Custom
return max(1,21,53,12,58,65,32,12,445,9382,132,12398,1239812, 21938721, 981, 91283, 1, 1, 1, 1, 1, 102183129312)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.max
Custom
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
gemma2:9b
, generated one year ago):
This benchmark compares the performance of JavaScript's built-in `Math.max()` function against a custom implementation of finding the maximum value from an array of numbers. **Options Compared:** * **`Math.max()`:** This is a native JavaScript function designed specifically to find the maximum value within a set of numbers provided as arguments. * **Custom Implementation:** The `max()` function defined in the script preparation code iterates through the provided arguments, comparing each with the current maximum value and updating it if a larger value is found. **Pros/Cons:** * **`Math.max()`:** * **Pros:** Simple to use, generally optimized by JavaScript engines. * **Cons:** Can be less flexible than a custom implementation if you need to handle specific edge cases or apply additional logic during the comparison. * **Custom Implementation:** * **Pros:** Potentially more control over the comparison process, allowing for customization based on your needs. * **Cons:** Might be slightly slower than `Math.max()` due to the manual iteration and comparison logic. **Other Considerations:** * In most cases, `Math.max()` is a good choice because it's concise and highly optimized by JavaScript engines. * A custom implementation might be preferred if you have specific requirements beyond simply finding the maximum value (e.g., handling negative numbers differently, comparing objects instead of numbers). **Alternatives:** * Other libraries like Lodash or Underscore provide functions for finding the maximum value (`_.max()`, `lodash.max()`), which often offer additional features and customization options. Let me know if you have any more questions!
Related benchmarks:
Math.max vs. custom algormithm
Javascript: reduce VS for with Math.max
array math.max (3 variants) vs for loop (4 variants)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?