Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
MinMax comparison
(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 = [1,2,3,4,5,6,7,8,9,67,234,null];
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 break down the benchmark and explain what's being tested, compared, and their pros and cons. **Overview** The benchmark is designed to compare two approaches for finding the minimum and maximum values in an array: "Reduce" (using `Array.prototype.reduce()`) and "Math + filter". The goal is to determine which approach is faster and more efficient. **Options Compared** 1. **Reduce**: This approach uses a callback function to iterate over the array, accumulating the minimum and maximum values in an object. 2. **Math + filter**: This approach uses the `Math.min()` and `Math.max()` functions to find the minimum and maximum values in the filtered array. **Pros and Cons** 1. **Reduce**: * Pros: Can handle non-numeric values, allows for more control over the iteration process. * Cons: May have a higher overhead due to object creation and callback function calls. 2. **Math + filter**: * Pros: Typically faster and more efficient, as it leverages built-in functions with optimized implementations. * Cons: Requires an array of non-null values, which may not be suitable for all use cases. **Library Used** None explicitly mentioned in the benchmark definition. However, both approaches rely on built-in JavaScript methods (`Array.prototype.reduce()` and `Math.min()`, `Math.max()`). **Special JS Features or Syntax** There are no special features or syntaxes being used in this benchmark. **Benchmark Preparation Code** The preparation code creates an array with a mix of numbers, null values, and other data types. This is likely done to ensure that the test cases handle non-numeric and null values correctly. **Individual Test Cases** Each test case has a unique benchmark definition that defines a separate function (`getMin`, `getMax`, `getMixMax`) or uses an existing one (`getMinMax`). The functions perform different operations on the input array, such as filtering out null values or reducing the array to find the minimum and maximum values. **Latest Benchmark Result** The latest result shows two test cases with Chrome 91 running on a Mac OS X 10.15.7 system, executing at approximately 85134 and 81733 executions per second for the "Reduce" and "Math + filter" approaches, respectively. **Other Alternatives** If you were to consider alternative approaches, some options might include: 1. **Using `Array.prototype.map()`**: Instead of filtering out null values, you could use `map()` to create a new array with non-null values. 2. **Using a third-party library**: There are libraries like Lodash or Ramda that provide optimized functions for finding minimum and maximum values in arrays. 3. **Using a streaming algorithm**: For large datasets, streaming algorithms can be more efficient than reducing the entire array. However, without further context or specific requirements, it's difficult to determine which alternative approach would be most suitable for a particular use case.
Related benchmarks:
arr.sort() vs. Math.min()
Array.sort vs Math.min+Math.max (LONG ARRAYS)
MinMax comparison 2
Sort method comparisons (quicksort, for loop, Arra.prototype.sort)
JS fastest unique array Set vs uniq vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?