Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
MinMax comparison 2
(version: 0)
Check what approach works better for MinMax function
Comparing performance of:
Reduce vs Math + filter
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, null, 91, 92, 93, 94, 95, 96, 97, 98, 99];
Tests:
Reduce
const getMin = (a, b) => a === null || (b !== null && a > b) ? b : a; const getMax = (a, b) => a === null || (b !== null && a < b) ? b : a; const getMixMax = (array) => array.reduce( ({ min, max }, value) => ({ min: getMin(min, value), max: getMax(max, value), }), { min: null, max: null }, ); const mixMax = getMixMax(arr); console.log(mixMax)
Math + filter
const getMinMax = data => !data.length ? [0, 0] : [Math.min(...data.filter(value => value !== null)), Math.max(...data.filter(value => value !== null))]; const minMax = getMinMax(arr); console.log(minMax)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Math + filter
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 world of JavaScript microbenchmarks! The provided JSON represents two test cases for comparing different approaches to finding the minimum and maximum values in an array. The tests are designed to measure the performance of these approaches on a specific dataset. **Test Case 1: Reduce** This test case uses the `reduce()` method to find the minimum and maximum values in the array. Here's what happens: * The `getMixMax` function is defined, which takes an array as input. * Inside `getMixMax`, the `reduce()` method is used to iterate over the array and accumulate the minimum and maximum values in an object. * The `min` and `max` properties of this object are updated based on the current value being processed. * Finally, the `mixMax` variable is assigned the result of the `getMixMax` function call. **Test Case 2: Math + filter** This test case uses a different approach to find the minimum and maximum values in the array. Here's what happens: * The `getMinMax` function is defined, which takes an array as input. * Inside `getMinMax`, the `filter()` method is used to remove any null values from the array. * Then, the `Math.min()` and `Math.max()` functions are used to find the minimum and maximum values in the filtered array. * The result of these two function calls is returned by the `getMinMax` function. **Pros and Cons:** * **Reduce:** This approach has a few advantages: + It's concise and easy to read. + It avoids the need for explicit loops or manual null checks. + However, it may not be as efficient as other approaches due to the overhead of the `reduce()` method. * **Math + filter:** This approach has some pros: + It's straightforward and easy to understand. + It avoids the use of mutable state (the accumulator object in `reduce()`). + However, it requires more code and may be less concise. **Library usage:** In both test cases, no external libraries are used. The standard JavaScript functions (`Math.min()`, `Math.max()`) and methods (`Array.prototype.filter()`, `Array.prototype.reduce()`) are used. **Special JS feature:** No special JavaScript features or syntax are used in these tests. They rely solely on standard JavaScript constructs. Now, let's talk about the benchmarking results: The latest results show that Chrome 91 on a Mac OS X 10.15.7 desktop executes the `Reduce` test case approximately 18% faster than the `Math + filter` test case (79644.42 executions per second vs 74098.03). **Alternatives:** Other approaches to finding minimum and maximum values in an array could include: * Using `Array.prototype.every()` and `Array.prototype.some()` to find all values that satisfy a certain condition. * Using `Array.prototype.forEach()` to iterate over the array and update variables manually. * Using a custom loop-based approach with manual null checks. These alternatives might be more suitable for specific use cases or performance-critical applications, but they require more code and may have different trade-offs in terms of readability and maintainability.
Related benchmarks:
Array: spread operator vs push #2
Array.sort vs Math.min+Math.max (LONG ARRAYS)
set.has vs. array.includes 3
Array filter versus for loop
Comments
Confirm delete:
Do you really want to delete benchmark?