Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs Array.reduce() xxxyyy
(version: 0)
Compare speed of Math.max() vs Array.reduce().
Comparing performance of:
Math.max vs Reduce vs Math.max spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = new Array(5000); for (let i = 0; i < values.length; ++i) { values[i] = i % 20; }
Tests:
Math.max
return Math.max.apply(Math, values);
Reduce
return values.reduce((prev, curr) => { if (curr > prev) return curr; return prev; }, 0);
Math.max spread
return Math.max(...values);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.max
Reduce
Math.max spread
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's being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark is defined as comparing the speed of three different ways to find the maximum value in an array: 1. `Math.max()` 2. `Array.reduce()` with a custom callback function 3. `Math.max(...)` (spread syntax) **Script Preparation Code** This code generates an array of 5000 random values, where each value is the remainder of its index divided by 20. ```javascript var values = new Array(5000); for (let i = 0; i < values.length; ++i) { values[i] = i % 20; } ``` **Html Preparation Code** This field is empty, indicating that no HTML preparation code is required for this benchmark. **Individual Test Cases** 1. **Math.max** The benchmark definition returns the result of `Math.max.apply(Math, values)`. ```javascript return Math.max.apply(Math, values); ``` 2. **Reduce** The benchmark definition returns the result of `values.reduce((prev, curr) => { ... }, 0)`. ```javascript return values.reduce((prev, curr) => { if (curr > prev) return curr; return prev; }, 0); ``` 3. **Math.max spread** The benchmark definition returns the result of `Math.max(...values)`. ```javascript return Math.max(...values); ``` **Latest Benchmark Result** The latest results show that: * The "Reduce" approach executed approximately 113,596 times per second on a Chrome 99 browser on a Windows desktop. * The "Math.max spread" approach executed approximately 60,564 times per second on the same browser and platform. * The "Math.max" approach executed approximately 56,416 times per second on the same browser and platform. **Options Compared** The three options are compared in terms of their execution speed. The results indicate that: * `Array.reduce()` is the fastest approach, followed by `Math.max()` with spread syntax. * `Math.max()` without spread syntax is slower than both of the above approaches. **Pros and Cons** 1. **Array.reduce()** * Pros: efficient use of array methods, flexible callback function * Cons: may require more code and setup compared to other options 2. **Math.max() with Spread Syntax** * Pros: concise and readable, faster execution than `Math.max()` without spread syntax * Cons: requires the spread operator (`...`) which may not be supported in older browsers or environments 3. **Math.max() without Spread Syntax** * Pros: widely supported across browsers and environments, simple to implement * Cons: slower execution compared to the above two options **Other Considerations** * The benchmark uses a fixed array size of 5000, which may not be representative of real-world scenarios where arrays can have varying sizes. * The use of random values in the array may introduce some randomness in the results, but it's likely that the general trend holds true for other array sizes and value distributions. * It's worth noting that these benchmarks are run on a specific browser and platform combination, which may not be representative of all possible environments.
Related benchmarks:
Math.max() vs Array.reduce.apply()
Math.max() vs Array.reduce() 4
Math.max vs Array.reduce
Get max from an array of numbers (Math.max vs. iteration) V3
Math.max() vs Array.reduce(Math.max)
Comments
Confirm delete:
Do you really want to delete benchmark?