0

How can I easily obtain the min and max values from a very big JavaScript Array (e.g 250 thousand numbers)?

I tried to use this method, but it does not work: JavaScript: min & max Array values?

I'm debugging in VisualStudio where I don't get any error, while in the browser I get "Maximum call stack size exceeded".

Example code:

var max = Math.max.apply(-Infinity, arr);
var min = Math.min.apply(Infinity, arr);

"arr" is my very big array.

Community
  • 1
  • 1
PAPP
  • 57
  • 10
  • First, first argument of apply is context. It should be `null` or `this`. Second you can always go back to custom for loop. That is the safest option – Rajesh Sep 02 '16 at 08:58
  • what's the problem with your code? In my chrome is working. – pietro909 Sep 02 '16 at 08:59
  • for me the code also works, what browser are you using ? – Willi Pasternak Sep 02 '16 at 09:00
  • I tried using null, this or Math, and the effect is always the same. – PAPP Sep 02 '16 at 09:03
  • @PAPP - I think it's worth mentioning what browser you are using so that others can have a chance of replicating your issue. – evolutionxbox Sep 02 '16 at 09:06
  • @evolutionxbox - I used it in Chrome, Firefox and Internet Explorer with the same result. It is important that this code works with smaller arrays (I tried with 65K elements). – PAPP Sep 02 '16 at 09:11
  • One certainly safe way of doing this job is using `Array.prototype.reduce()`. – Redu Sep 02 '16 at 09:12
  • @PAPP this is interesting: might be a performance issue? What you mean with "doesn't work"? – pietro909 Sep 02 '16 at 09:16
  • @pietro909 I used Visual Studio as a debugger, and I did not have any error message. But now I checked using tools from the browser, and there is an error: "Maximum call stack size exceeded". – PAPP Sep 02 '16 at 09:33
  • so you got it: there is a limit. Turns out that you can't do that. – pietro909 Sep 02 '16 at 09:36
  • I suspect that is using recursion under the hood. – pietro909 Sep 02 '16 at 09:38

2 Answers2

0

For large arrays of mentioned sizes you can not use Math.min() or Math.max() safely. I believe this is how it should be done.

var arr = new Array(1000000).fill().map(_=> ~~(Math.random()*10000000)),
findMax = a => a.reduce((res,cur) => res < cur ? cur : res ,-Infinity);
console.log(findMax(arr));
Redu
  • 25,060
  • 6
  • 56
  • 76
-1

It is working fine for me

var min = Math.min.apply(Math, array);
var max = Math.max.apply(Math, array);
Suresh Gogu
  • 359
  • 5
  • 12
  • 1
    I tried using null, this or Math, and the effect is always the same. Are you sure you tried to use it on such a large array as I described? Because this code works with smaller tables (I tried with 65K elements). – PAPP Sep 02 '16 at 09:06
  • I didn't tried with large arrays, let me check once @PAPP – Suresh Gogu Sep 02 '16 at 09:09
  • There is no guarantee when you need to pass arguments in the order of 150-300 K that you won't get stack overflow depending on the browser session. Large arrays can not be done by `Math.min()` or `Math.max()` – Redu Sep 02 '16 at 09:11