Find the min/max element of an Array in JavaScript

前端 未结 30 2044
無奈伤痛
無奈伤痛 2020-11-21 06:18

How can I easily obtain the min or max element of a JavaScript Array?

Example Psuedocode:

let array = [100, 0, 50]

array.min() //=> 0
array.max()         


        
相关标签:
30条回答
  • 2020-11-21 06:40

    Using spread operator (ES6)

    Math.max(...array);  // the same with "min" => Math.min(...array);
    

    const array = [10, 2, 33, 4, 5];
    
    console.log(
      Math.max(...array)
    )

    0 讨论(0)
  • 2020-11-21 06:40

    tl;dr

    // For regular arrays:
    var max = Math.max(...arrayOfNumbers);
    
    // For arrays with tens of thousands of items:
    let max = testArray[0];
    for (let i = 1; i < testArrayLength; ++i) {
      if (testArray[i] > max) {
        max = testArray[i];
      }
    }
    

    MDN solution

    The official MDN docs on Math.max() already covers this issue:

    The following function uses Function.prototype.apply() to find the maximum element in a numeric array. getMaxOfArray([1, 2, 3]) is equivalent to Math.max(1, 2, 3), but you can use getMaxOfArray() on programmatically constructed arrays of any size.

    function getMaxOfArray(numArray) {
        return Math.max.apply(null, numArray);
    }
    

    Or with the new spread operator, getting the maximum of an array becomes a lot easier.

    var arr = [1, 2, 3];
    var max = Math.max(...arr);
    

    Maximum size of an array

    According to MDN the apply and spread solutions had a limitation of 65536 that came from the limit of the maximum number of arguments:

    But beware: in using apply this way, you run the risk of exceeding the JavaScript engine's argument length limit. The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function. To illustrate this latter case: if such an engine had a limit of four arguments (actual limits are of course significantly higher), it would be as if the arguments 5, 6, 2, 3 had been passed to apply in the examples above, rather than the full array.

    They even provide a hybrid solution which doesn't really have good performance compared to other solutions. See performance test below for more.

    In 2019 the actual limit is the maximum size of the call stack. For modern Chromium based desktop browsers this means that when it comes to finding min/max with apply or spread, practically the maximum size for numbers only arrays is ~120000. Above this, there will be a stack overflow and the following error will be thrown:

    RangeError: Maximum call stack size exceeded

    With the script below (based on this blog post), by catching that error you can calculate the limit for your specific environment.

    Warning! Running this script takes time and depending on the performance of your system it might slow or crash your browser/system!

    let testArray = Array.from({length: 10000}, () => Math.floor(Math.random() * 2000000));
    for (i = 10000; i < 1000000; ++i) {
      testArray.push(Math.floor(Math.random() * 2000000));
      try {
        Math.max.apply(null, testArray);
      } catch (e) {
        console.log(i);
        break;
      }
    }

    Performance on large arrays

    Based on the test in EscapeNetscape's comment I created some benchmarks that tests 5 different methods on a random number only array with 100000 items.

    In 2019, the results show that the standard loop (which BTW doesn't have the size limitation) is the fastest everywhere. apply and spread comes closely after it, then much later MDN's hybrid solution then reduce as the slowest.

    Almost all tests gave the same results, except for one where spread somewhy ended up being the slowest.

    If you step up your array to have 1 million items, things start to break and you are left with the standard loop as a fast solution and reduce as a slower.

    JSPerf benchmark

    JSBen benchmark

    JSBench.me benchmark

    Benchmark source code

    var testArrayLength = 100000
    var testArray = Array.from({length: testArrayLength}, () => Math.floor(Math.random() * 2000000));
    
    // ES6 spread
    Math.min(...testArray);
    Math.max(...testArray);
    
    // reduce
    testArray.reduce(function(a, b) {
      return Math.max(a, b);
    });
    testArray.reduce(function(a, b) {
      return Math.min(a, b);
    });
    
    // apply
    Math.min.apply(Math, testArray);
    Math.max.apply(Math, testArray);
    
    // standard loop
    let max = testArray[0];
    for (let i = 1; i < testArrayLength; ++i) {
      if (testArray[i] > max) {
        max = testArray[i];
      }
    }
    
    let min = testArray[0];
    for (let i = 1; i < testArrayLength; ++i) {
      if (testArray[i] < min) {
        min = testArray[i];
      }
    }
    
    // MDN hibrid soltuion
    // Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply#Using_apply_and_built-in_functions
    function minOfArray(arr) {
      var min = Infinity;
      var QUANTUM = 32768;
    
      for (var i = 0, len = arr.length; i < len; i += QUANTUM) {
        var submin = Math.min.apply(null, arr.slice(i, Math.min(i + QUANTUM, len)));
        min = Math.min(submin, min);
      }
    
      return min;
    }
    
    minOfArray(testArray);
    
    function maxOfArray(arr) {
      var max = -Infinity;
      var QUANTUM = 32768;
    
      for (var i = 0, len = arr.length; i < len; i += QUANTUM) {
        var submax = Math.max.apply(null, arr.slice(i, Math.max(i + QUANTUM, len)));
        max = Math.max(submax, max);
      }
    
      return max;
    }
    
    maxOfArray(testArray);

    0 讨论(0)
  • 2020-11-21 06:42

    Simple stuff, really.

    var arr = [10,20,30,40];
    arr.max = function() { return  Math.max.apply(Math, this); }; //attach max funct
    arr.min = function() { return  Math.min.apply(Math, this); }; //attach min funct
    
    alert("min: " + arr.min() + " max: " + arr.max());
    
    0 讨论(0)
  • 2020-11-21 06:47

    Alternative Methods


    The Math.min and Math.max methods are both recursive operations that being added to the JS engine's call stack, and most likely crash for an array that contains large number of items
    (more than ~10⁷ items, depends on the user's browser).

    Math.max(...Array(1000000).keys());

    Uncaught RangeError: Maximum call stack size exceeded

    Instead, use something like so:

    arr.reduce((max, val) => max > val ? max : val, arr[0])
    

    Or with better run-time:

    function maxValue(arr) {
      let max = arr[0];
    
      for (let val of arr) {
        if (val > max) {
          max = val;
        }
      }
      return max;
    }
    

    Or to get both Min and Max:

    function getMinMax(arr) {
      return arr.reduce(({min, max}, v) => ({
        min: min < v ? min : v,
        max: max > v ? max : v,
      }), { min: arr[0], max: arr[0] });
    }
    

    Or with even better run-time*:

    function getMinMax(arr) {
      let min = arr[0];
      let max = arr[0];
      let i = arr.length;
        
      while (i--) {
        min = arr[i] < min ? arr[i] : min;
        max = arr[i] > max ? arr[i] : max;
      }
      return { min, max };
    }
    

    * Tested with 1,000,000 items:
    Just for a reference, the 1st function run-time (on my machine) was 15.84ms vs 2nd function with only 4.32ms.

    0 讨论(0)
  • 2020-11-21 06:47

    I am surprised not one mentiond the reduce function.

    var arr = [1, 10, 5, 11, 2]
    
    var b = arr.reduce(function(previous,current){ 
                          return previous > current ? previous:current
                       });
    
    b => 11
    arr => [1, 10, 5, 11, 2]
    
    0 讨论(0)
  • 2020-11-21 06:47

    For big arrays (~10⁷ elements), Math.min and Math.max procuces a RangeError (Maximum call stack size exceeded) in node.js.

    For big arrays, a quick & dirty solution is:

    Array.prototype.min = function() {
        var r = this[0];
        this.forEach(function(v,i,a){if (v<r) r=v;});
        return r;
    };
    
    0 讨论(0)
提交回复
热议问题