Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get max from an array of numbers (Math.max vs. iteration)
(version: 0)
Comparing performance of:
Math.max vs Find
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [...Array(1000)].map(() => Math.floor(Math.random() * 100000));
Tests:
Math.max
return Math.max(...arr);
Find
return arr.reduce((maxi, curr) => maxi < curr ? curr : maxi, -1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.max
Find
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):
**Overview of the Benchmark** The provided benchmark measures the performance difference between two approaches to find the maximum value in an array: using the built-in `Math.max` function and iterating through the array manually. **Approaches Compared** There are two test cases: 1. **Math.max**: This approach uses the built-in `Math.max` function, which is a part of the JavaScript standard library. It takes multiple arguments and returns the largest value among them. 2. **Find**: This approach uses a manual iteration through the array to find the maximum value. It starts with an initial value (`-1`) and updates it if it finds a larger value in the array. **Pros and Cons of Each Approach** * **Math.max**: + Pros: Fast, efficient, and concise. + Cons: May not be as educational or illustrative of fundamental concepts, as it relies on a built-in function. * **Find**: + Pros: Educational, illustrating the importance of iterating through arrays to find maximum/minimum values. + Cons: Slower and more verbose compared to using `Math.max`. **Library Used** None. **Special JavaScript Features/Syntax** There are no special features or syntax used in this benchmark. It only focuses on demonstrating two simple approaches to finding the maximum value in an array. **Other Alternatives** If you wanted to measure alternative approaches, some possible options could include: 1. Using `reduce()` instead of manual iteration. 2. Comparing performance with a custom algorithm using recursion or other methods. 3. Measuring the impact of array size on performance (e.g., 1000 elements vs. 10,000 elements). Keep in mind that these alternatives would require significant changes to the benchmark setup and test cases. **Benchmark Preparation Code Explanation** The provided script preparation code generates an array of 1000 random numbers using `Array.prototype.map()` and assigns it to the variable `arr`. This ensures that each test case starts with a fresh, randomized array. The individual test cases are defined as JavaScript functions that return the desired result: either using `Math.max` or implementing the manual iteration approach.
Related benchmarks:
Get max from an array of numbers (Math.max vs. iteration) V2
Get max from an array of numbers (Math.max vs. iteration) V3
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?