1

Here i have an array with elements whose indexes are separated by increments of 1.

let noGap = [];
noGap[0] = 0;
noGap[1] = 1;

Here I have a different array whose indexes are separated by indexes of much greater than 1.

let gap = [];
gap[0] = 0;
gap[1000] = 1;

What is the difference between how much memory each variable (noGap vs gap) uses? I can see from a Chrome console log that gap has wider length, because it logs (1001) [0, undefined × 999, 1].

But if the gap variable indeed uses more space, I'm interested to know if it's proportionate to the number of undefined that exist in the array or if it's constant.

Pardon me if this is repeated question. This is the closest answer I found but I couldn't fully understand the answer.

lukkyjoe
  • 304
  • 4
  • 13
  • 3
    "*…because there are 999 'undefined' in there*". No, there aren't. There are two members and a length of 1001. – RobG Jun 06 '17 at 22:55
  • https://stackoverflow.com/questions/20321047/how-are-javascript-arrays-represented-in-physical-memory – Tommy May Jun 06 '17 at 23:00
  • Really depends on the JS engine and the number of times this code is run - see hotspot optimization / optimizing compiler. The second array with holes will probably be backed by a hash table with certain overhead while the first will be stored as a compact 32-bit signed integer array. Both perform preallocation. – le_m Jun 06 '17 at 23:25
  • Internet Exploder shows `0` then 999 actual `undefined` values then `1` - firefox shows `0, 999 empty slots, 1`, chrome says `0, 999 x undefined, 1` (is that how chrome shows empty slots?) - so ... which browser are you referring to – Jaromanda X Jun 06 '17 at 23:32
  • thanks, all. i used chrome. edited my post for clarity. also thank you @TommyMay I knew there was a preceding post somewhere that I probably missed. – lukkyjoe Jun 07 '17 at 03:23

1 Answers1

0

In V8, there is a big difference, the long and sparse array takes more memory. However, this is partially because 1000 is near the boundary after which V8 changes its internal representation of sparse arrays. An even longer sparse array will likely take a similar amount of memory.

I only did a little evidence gathering with the Chrome dev tools (check out the memory tab), but here it is. Each row in this table is an array with an entry at index 0, and another at the end to define its length.

| sparse array length | retained size |
|---------------------|---------------|
| 1                   | 56            |
| 10                  | 304           |
| 1000                | 12,184        |
| 2000                | 184           |
| 3000                | 184           |
| 3000, less sparse   | 472           |

Notice that memory scales with length up to 1000, but at 2000 it's much less, and it scales with number of set indices instead.

I'm assuming this is because V8 is switching from a complete representation of all the values from indices 0 to length (in the case of length 1000) to a hash map which only has entries for the indices which have been set.

I have no formal knowledge of how this actually works, FYI.

Chris
  • 6,805
  • 3
  • 35
  • 50