Can you help me to make this code works with more than 100k entries.
var maxDate = new Date(Math.max.apply(null, dates));
var minDate = new Date(Math.min.apply(null, dates));
For now I have this error RangeError: Maximum call stack size exceeded.
Thanks for your help
I think it's a recursion problem here. It seems the stack used in recursion has a maximum size that's why the Math.min and Math.max most likely crash for big arrays because they are both recursive operations.
Instead, you can use old javascript loops like so:
function getMax(arr) {
return arr.reduce((max, v) => max >= v ? max : v, -Infinity);
}
Or
function getMax(arr) {
let len = arr.length;
let max = -Infinity;
while (len--) {
max = arr[len] > max ? arr[len] : max;
}
return max;
}
(The second is much faster)
I think,your dates array is not correct; you can check this code:
var dates=[]; for(var i=0;i<100001;i++){ dates.push(randomDate(new Date(2018, 0, 1), new Date())); } var max = new Date(Math.max.apply(null, dates)); var min = new Date(Math.min.apply(null, dates)); \\\\console.log(dates); console.log('Max is:'+max); console.log('Min is:'+min); function randomDate(start, end) { return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.