# In JavaScript, find the array’s minimum and maximum elements.

## Problem

How can I get the min and max elements of a JavaScript array quickly?

Example pseudocode:

``````let array = [100, 0, 50]

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

## Solution #1

Instead of using the built-in Array object, you may use Math.max/Math.min:

``````Array.prototype.max = function() {
return Math.max.apply(null, this);
};

Array.prototype.min = function() {
return Math.min.apply(null, this);
};
``````

Here’s a JSFiddle to play with.

Because using the built-ins can cause conflicts with other libraries (as some people have discovered), you might be better off applying Math.xxx() directly to your array:

``````var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
``````

If your browser supports ECMAScript 6, you can also use the spread operator, which works in the same way as the apply method:

``````var min = Math.min( ...arr ),
max = Math.max( ...arr );
``````

## Solution #2

``````var max_of_array = Math.max.apply(Math, array);
``````

See http://aaroncrane.co.uk/2008/11/javascript max api/ for further information.

## Solution #3

Both Math.min and Math.max in Node.js show the following error for large arrays (107 members).

Instead of adding every member to the call stack, a more robust alternative is to pass an array:

``````function arrayMin(arr) {
return arr.reduce(function (p, v) {
return ( p < v ? p : v );
});
}

function arrayMax(arr) {
return arr.reduce(function (p, v) {
return ( p > v ? p : v );
});
}
``````

If performance is a problem, the following code is 3 times faster on my machine than Math.max.apply. See http://jsperf.com/min-and-max-in-array/2 for further information.

``````function arrayMin(arr) {
var len = arr.length, min = Infinity;
while (len--) {
if (arr[len] < min) {
min = arr[len];
}
}
return min;
};

function arrayMax(arr) {
var len = arr.length, max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
};
``````

If your arrays contains strings instead of numbers, you also need to coerce them into numbers. The below code does that, but it slows the code down ~10 times on my machine. See http://jsperf.com/min-and-max-in-array/3.

``````function arrayMin(arr) {
var len = arr.length, min = Infinity;
while (len--) {
if (Number(arr[len]) < min) {
min = Number(arr[len]);
}
}
return min;
};

function arrayMax(arr) {
var len = arr.length, max = -Infinity;
while (len--) {
if (Number(arr[len]) > max) {
max = Number(arr[len]);
}
}
return max;
};
``````

## Solution #4

``````Math.max(...array)  // The same with "min" => Math.min(...array)
``````

## Solution #5

``````// For regular arrays:
var max = Math.max(...arrayOfNumbers);

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

This is previously addressed in the official MDN docs for Math.max():

According to MDN, the maximum number of arguments for the apply and spread solutions was 65536, which derived from the limit of the maximum number of arguments:

They even provide a hybrid solution that isn’t really effective in comparison to other options. For further information, see the performance test below.

The actual limit in 2019 is the call stack’s maximum size. For contemporary Chromium-based desktop browsers, this means that when using apply or spread to find min/max, the maximum size for numbers only arrays is effectively 120000. A stack overflow will occur above this point, resulting in the following error:

By capturing that error with the script below (based on this blog article), 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!

I produced some benchmarks based on EscapeNetscape’s test, which tests 5 alternative ways on a random number only array of 100000 entries.

The standard loop (which, by the way, does not have a size constraint) is the fastest worldwide in 2019. Apply and spread is next, followed by MDN’s hybrid solution, which is the slowest to minimize.

Almost all of the tests yielded the same findings, with the exception of one in which the spread was the slowest for some reason.

When you have a million items in your array, things start to break, and you’re left with the usual loop as a fast option and reduce as a slower one.