Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs Array.reduce() vs for of
(version: 0)
Compare speed of Math.max() vs Array.reduce().
Comparing performance of:
Math.max vs Reduce vs Reduce Max.max vs for of vs for vs for Math.max
Created:
3 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(...values);
Reduce
return values.reduce((prev, curr) => { if (curr > prev) return curr; return prev; }, 0);
Reduce Max.max
return values.reduce((prev, curr) => Math.max(prev, curr), 0);
for of
let mx = values[0]; for (const x of values) { if (mx < x) { mx = x; } } return mx;
for
let mx = values[0]; for (let i = 1; i < values.length; ++i) { if (values[i] > mx) { mx = values[i]; } } return mx;
for Math.max
let mx = values[0]; for (let i = 1; i < values.length; ++i) { mx = Math.max(values[i], mx); } return mx;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Math.max
Reduce
Reduce Max.max
for of
for
for Math.max
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 explanation of what is tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of three different approaches to find the maximum value in an array: 1. `Math.max()` 2. `Array.reduce()` with a custom reduction function 3. A traditional `for` loop and a conditional update **Comparison Options** There are four options being compared: 1. **`Math.max()`**: This is a built-in JavaScript function that returns the largest of zero or more numbers. 2. **`Array.reduce()`**: This is a method that applies a reduction function to each element in an array, reducing it to a single output value. 3. **Traditional `for` loop with conditional update**: This approach uses a traditional `for` loop to iterate over the array and updates the maximum value conditionally. 4. **`Array.reduce()` with a custom `Math.max()` implementation**: This is a variation of option 2, where the reduction function uses the built-in `Math.max()` function. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **`Math.max()`**: + Pros: Simple, efficient, and widely supported. + Cons: May not be suitable for non-numeric values or edge cases. * **`Array.reduce()`**: + Pros: Flexible, can handle non-numeric values, and can be optimized with a custom reduction function. + Cons: May have performance overhead due to the creation of an intermediate array. * **Traditional `for` loop**: + Pros: Can be more intuitive for some developers, less memory-intensive than `Array.reduce()`. + Cons: More verbose, prone to errors, and may not be as performant. **Library Usage** The benchmark uses the `reduce()` method, which is a part of the ECMAScript standard. The purpose of this method is to reduce an array to a single output value by applying a reduction function to each element in the array. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax that would affect their performance. **Other Alternatives** If you were to implement a similar benchmark, other alternatives could be: * Using `Array.prototype.every()` and `Math.max()` * Using `Array.prototype.indexOf()` and `Math.max()` * Implementing a custom reduction function using recursion * Using a different language or framework for the benchmark Keep in mind that these alternatives might not provide the same level of performance or generality as the original options. Overall, this benchmark provides a useful comparison of four approaches to find the maximum value in an array. It highlights the trade-offs between simplicity, flexibility, and performance.
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?