I was wondering if any one knows how memory is handled with JS arrays if you have an array that starts with a high value.
For example, if you have:
array[5000] = 1;
As the first value in the array, everything before 5000 simply does not exist, will the amount of memory assigned to the array cater for the unassigned 4999 positions prior to it… or will it only assign memory to the value in the array for [5000] ?
I’m trying to cut down on the amount of memory used for my script so this led to me wondering about this question 🙂
When assigning a value to the
5000th key, not the whole array is populated:If you want to blow your new browser with arrays, populate a typed array:
Both can be verified easily in Chrome: Open the console and memory console (Shift+Esc), and paste the code.
window.a=new Array(6e9);orwindow.a=[];window[6e9]=1;doesn’t result in a significant memory increase,while
window.a=new ArrayBuffer(6e9);crashes the page.PS.
6e9 === 6000000000