实现计数排序时需要取到数组极值,以下为实现取数组最大值的方式,最佳方式为
const max = arr.reduce((a, b) => Math.max(a, b), -Infinity);
Array.reduce()
最佳方式
Array.prototype.reduce() can be used to find the maximum element in a numeric array, by comparing each value:
var arr = [1,2,3];
var max = arr.reduce(function(a, b) {
return Math.max(a, b);
}, 0);
Math.max.apply()
The following function uses Function.prototype.apply() to get the maximum of an array. getMaxOfArray([1, 2, 3])
is equivalent to Math.max(1, 2, 3)
, but you can use getMaxOfArray()
on programmatically constructed arrays. This should only be used for arrays with relatively few elements.
function getMaxOfArray(numArray) {
return Math.max.apply(null, numArray);
}
spread operator
The new spread operator is a shorter way of writing the apply
solution to get the maximum of an array:
var arr = [1, 2, 3];
var max = Math.max(...arr);
However, both spread (...
) and apply
will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters. See Using apply and built-in functions for more details. The reduce
solution does not have this problem.
Using apply and built-in functions
Clever usage of apply
allows you to use built-in functions for some tasks that would probably have otherwise been written by looping over the array values.
As an example, here are Math.max
/Math.min
, used to find out the maximum/minimum value in an array.
// min/max number in an array
const numbers = [5, 6, 2, 3, 7];
// using Math.min/Math.max apply
let max = Math.max.apply(null, numbers);
// This about equal to Math.max(numbers[0], ...)
// or Math.max(5, 6, ...)
let min = Math.min.apply(null, numbers);
// vs. simple loop based algorithm
max = -Infinity, min = +Infinity;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}
But beware: by 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 (that is, more than tens of thousands of arguments) varies across engines. (The JavaScriptCore engine has hard-coded argument limit of 65536.
This is because the limit (and 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.
If your value array might grow into the tens of thousands, use a hybrid strategy: apply your function to chunks of the array at a time:
function minOfArray(arr) {
let min = Infinity;
let 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;
}
let min = minOfArray([5, 6, 2, 3, 7]);