Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs Array.reduce() vs for element of array loop
(version: 0)
Compare speed of Math.max() vs Array.reduce().
Comparing performance of:
Math.max vs Reduce vs for element of array
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(...values);
Reduce
return values.reduce((prev, curr) => { if (curr > prev) return curr; return prev; }, 0);
for element of array
let biggest = values[0]; for (let value of values) { if (value > biggest) biggest = value; }; return biggest;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.max
Reduce
for element of array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 17 on iOS 17.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
79026.2 Ops/sec
Reduce
185638.4 Ops/sec
for element of array
210109.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the speed of three different approaches to find the maximum value in an array: 1. `Math.max()`: This function takes an array as an argument and returns the highest value in the array. 2. `Array.reduce()`: This method applies a reduction operation to the array, which is a way to combine all elements into a single output value. In this case, it finds the maximum value by comparing each element with the previous result. 3. `for` loop: A traditional for loop that iterates over the array and keeps track of the maximum value found so far. **Options Compared** The benchmark compares the speed of these three approaches: * `Math.max()`: a built-in JavaScript function * `Array.reduce()`: a built-in JavaScript method * `for` loop: a traditional, iterative approach **Pros and Cons** Here's a brief summary of each approach: * `Math.max()`: fast and concise, but may not be as efficient for very large arrays. + Pros: easy to read and write, fast execution time. + Cons: may consume more memory, limited control over the iteration process. * `Array.reduce()`: flexible and expressive, but may have performance overhead due to function call overhead. + Pros: versatile, can be used for various reduction operations. + Cons: slower than a simple loop, may consume more memory. * `for` loop: straightforward and efficient, but requires manual bookkeeping of indices and comparisons. + Pros: fast execution time, low memory consumption, easy to control iteration flow. + Cons: verbose and less readable than other approaches. **Library Used** The test cases use the following libraries: None. The test cases only rely on built-in JavaScript methods and features. **Special JS Features/Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Other Considerations** When choosing an approach, consider the trade-offs between speed, memory consumption, and readability. If you need a simple, fast solution for small to medium-sized arrays, `Math.max()` might be a good choice. For more complex reduction operations or very large arrays, `Array.reduce()` or a traditional `for` loop might be more suitable. **Alternatives** If you're interested in exploring other alternatives, consider the following: * Using `Math.min()` instead of `Math.max()` for an opposite operation. * Implementing a custom algorithm using bitwise operations, such as using `Integer.MAX_VALUE` to find the maximum value. * Using a library like Lodash or Ramda, which provide additional functional programming utilities and optimizations. Keep in mind that the best approach will depend on your specific use case and performance requirements.
Related benchmarks:
Math.max() vs Array.reduce() 4
Math.max() vs Array.reduce() vs For-loop
Math.max() vs Array.reduce(Math.max)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?