how to avoid stack overflow when applying max to a large array in javascript? -


the following code

var interval = function (a, b) {           var i, list = [];           (i = a; <= b; i++) {             list.push(i);           }           return list;         },     xs = interval(1, 500000);  math.max.apply(null, xs); 

generates uncaught rangeerror: maximum call stack size exceeded. how overcome?

note interval function quick way generate test data.

i used math.max.apply method because described here: mozilla developer network

this not acceptable solution because javascript has maximum number of arguments allowed function call, rocket hazmat pointing out, see answer more informations.

the underscore.js library uses simple implementation max function, , believe appropriate solution include simple max implementation in codebase , use it. see @anotherdev answer more details

the issue here line:

math.max.apply(null, xs); 

you trying call math.max(1, 2, 3, 4, ..., 500000);. javascript doesn't calling function 500,000 parameters.

see answer more info: https://stackoverflow.com/a/22747272


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -