Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max.apply() vs Array.reduce()
(version: 0)
Compare speed of Math.max() vs Array.reduce().
Comparing performance of:
Math.max vs Reduce
Created:
3 years ago
by:
Guest
Go 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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.max
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Reduce
225K/s
Math.max
63K/s
View exact numbers
Test name
Executions per second
✓
Reduce
224,791 Ops/sec
Math.max
62,760 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its results. **Benchmark Definition** The benchmark compares the performance of two JavaScript functions: `Math.max.apply()` and `Array.reduce()`. The script preparation code generates an array of 5,000 numbers between 0 and 19 (inclusive), which will be used to test these functions. The `Html Preparation Code` is empty, indicating that no additional HTML setup is needed for this benchmark. **Options Compared** The two options being compared are: 1. **Math.max.apply()**: This function applies the `Math.max()` function to an array of values using the `apply()` method. It's a more traditional way of finding the maximum value in an array. 2. **Array.reduce()**: This function reduces the array by repeatedly applying a callback function, which finds the maximum value. **Pros and Cons** Here are some pros and cons of each approach: * **Math.max.apply()**: + Pros: Simpler to understand and implement, as it's based on a well-known function. + Cons: May be slower than `Array.reduce()` because of the overhead of using `apply()`. * **Array.reduce()**: + Pros: More efficient in terms of memory usage and potentially faster execution time due to its optimized implementation. + Cons: Requires understanding of the `reduce()` method, which can be less intuitive for some developers. **Library and Purpose** The `Math.max()` function is a built-in JavaScript function that returns the largest value among a set of numbers. The `Array.prototype.reduce()` method is another built-in function that reduces an array by applying a callback function to each element. **Special JS Feature or Syntax** There are no special features or syntax mentioned in this benchmark. Both functions rely on basic JavaScript concepts and do not use any advanced features like ES6 syntax, async/await, or web worker APIs. **Other Alternatives** If you're interested in exploring alternative approaches for finding the maximum value in an array, here are a few options: * **For...of loop**: You can use a `for...of` loop to iterate over the array and keep track of the maximum value. This approach is similar to `Array.reduce()` but may be slower due to the additional overhead. * **`Math.max()` with multiple arguments**: If you know that the array contains only positive numbers, you can use `Math.max()` with multiple arguments to find the maximum value in a more concise way. Example: ```javascript const maxValue = Math.max(...values); ``` Keep in mind that this approach assumes that all elements in the array are positive numbers.
Related benchmarks
Math.max() vs Array.reduce.apply()
Math.max() vs Array.reduce() vs for element of array 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?