I have a small loop of code which is throwing Uncaught RangeError: Invalid Array Length
I was able to reproduce it with just this in the Google Chrome console
const COUNT = 100_000_000;
const xValues = new Array(COUNT);
const yValues = new Array(COUNT);
for (let i = 0; i < COUNT; i++) {
xValues[i] = i;
yValues[i] = Math.sin(i * 0.000001);
}
console.log(`count: ${yValues.length}`);
Here’s the output in developer console
As far as I know the maximum array size in Javascript is 2^32-1? There should be enough memory to allocate here and the index i is never negative or outside the bounds of the array as far as I can see.
Curiously enough, if I use this code, there is no crash
const COUNT = 100_000_000;
const xValues = new Array(COUNT);
const yValues = new Array(COUNT);
for (let i = 0; i < COUNT; i++) {
xValues[i] = i;
yValues[i] = i;
}
console.log(`count: ${yValues.length}`);
The value assigned to yValues[i] never goes outiside of the range -1, +1 so I can’t see this as a number out of range problem either.
Anyone shed any light on this?