Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() without spread vs reduce vs apply
(version: 0)
Comparing performance of:
Math.max vs Reduce vs Apply
Created:
one year 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((a, b) => Math.max(a, b), -Infinity);
Apply
function getMaxOfArray(numArray) { return Math.max.apply(null, numArray); } return getMaxOfArray(values);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.max
Reduce
Apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
16023.4 Ops/sec
Reduce
1698.1 Ops/sec
Apply
65571.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **What is being tested?** The benchmark tests three different ways to find the maximum value in an array: 1. `Math.max()`: using the built-in `max` function 2. `Array.prototype.reduce()` with a custom callback function: finding the maximum value by reducing the array to a single value. 3. `Function.apply(null, ...)`: using the `apply` method to call a function with an array of arguments. **Options compared** The benchmark compares the performance of these three approaches: * `Math.max()`: a simple and efficient way to find the maximum value in an array. * `Array.prototype.reduce()` with a custom callback function: a more complex approach that involves iterating over the array to find the maximum value. * `Function.apply(null, ...)`: a dynamic approach that uses the `apply` method to call a function with an array of arguments. **Pros and Cons** Here are some pros and cons for each approach: 1. **Math.max()**: * Pros: simple, efficient, and widely supported. * Cons: may not be suitable for very large arrays or arrays with non-numeric values. 2. **Array.prototype.reduce()` with a custom callback function: * Pros: can be used to perform more complex operations on the array. * Cons: may have performance overhead due to the iteration and callback function invocation. 3. **Function.apply(null, ...)**: * Pros: dynamic and flexible approach that can handle arrays of different lengths and data types. * Cons: may have higher performance overhead due to the `apply` method call. **Library** None of these approaches rely on external libraries. **Special JS features or syntax** The benchmark uses some special JavaScript features: 1. **Function.apply()**: the `apply` method is used to call a function with an array of arguments. 2. **Array.prototype.reduce()**: this method is used to reduce the array to a single value by iterating over its elements. **Other considerations** When choosing between these approaches, consider the specific requirements of your use case: * If you need a simple and efficient way to find the maximum value in an array, `Math.max()` is likely the best choice. * If you need to perform more complex operations on the array or handle non-numeric values, `Array.prototype.reduce()` with a custom callback function may be a better option. * If you need a dynamic approach that can handle arrays of different lengths and data types, `Function.apply(null, ...)` may be suitable. **Other alternatives** Some other alternatives to these approaches include: 1. Using the `Math.max()` function with multiple arguments (e.g., `Math.max(...values)`). 2. Using a library like Lodash or Ramda, which provide more functional programming-style functions for working with arrays. 3. Implementing your own custom algorithm using loops and conditional statements. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
math.max.apply vs math.max(...)
Javascript: reduce VS for with Math.max
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?