3 ms·
I used his technique for a while but then I had to find the max and min of an array of 1 million values which is more than the maximum number of arguments to a
by nkassis 16y ago
I used his technique for a while but then I had to find the max and min of an array of 1 million values which is more than the maximum number of arguments to a function in most javascript implementation so I rewrote it a such:
Array.prototype.min = function() {
var increment = 50000;
if(this.length > increment){
var reduced_array = [];
for(var i=0;i<this.length;i+=increment) {
reduced_array.push(Math.min.apply(Math,
this.slice(i,i+increment-1)));
}
}else {
return Math.min.apply(Math, this);
}
return reduced_array.min();
};
Array.prototype.max = function(array) {
var increment = 50000;
if(this.length > increment){
var reduced_array = [];
for(var i=0;i<this.length;i+=increment) {
reduced_array.push(Math.max.apply(Math, this.slice(i,i+increment-1)));
}
}else {
return Math.max.apply(Math, this);
}
I'm sure someone can suggest a better way.
- qixxiq 16y agoThey way you've done it seems fine, although if you're trying to find minimums of such large arrays in javascript I'd assume you're doing something wrong. Javascript really isn't a great choice of language for big processing tasks; stuff like that should be moved away where possible (granted it isn't always an option).
- nkassis 16y agoYeah I would not be doing it unless it was necessary. When I wrote that I was parsing a large file using javascript (from the client filesystem) and the round trip to the server would have been longer than this little function takes.