I am wondering how v8 solves the problem of storing arrays in a fragmented memory. Basically, what is the array data structure in v8. I assume under the hood v8 has to deal with the problem of memory fragmentation. I have read that C allocated arrays with contiguous memory which makes sense since you are allocating it directly anyways. But with JavaScript it is dynamic so it seems you can't allocate them contiguously always.
Given memory blocks of 8 bytes free ○ and allocated ●, imagine this scenario.
○○○○○○○○○○○○○○○○○○○○○○○○○○○
Then you add an array of 5 items:
●●●●●○○○○○○○○○○○○○○○○○○○○○○
Then you add another array to a different part of memory:
●●●●●○○○○◖◖◖○○○○○○○○○○○○○○○
The question is, if you add 10 more items to the first array, how does it work:
●●●●●●●●●◖◖◖●●●●●●○○○○○○○○○
Wondering if you are keeping track of the array structure somewhere else instead of just the fact that they are contiguous (like in C).
V8 developer here. Every array (both sparse/dictionary and dense/array mode) has one backing store for elements. When more elements are added than the backing store can hold, a new backing store is allocated and all elements are copied over. In this event the backing store is grown by a factor (not just one element), which is what gives you amortized-constant element addition performance. When not enough memory (or contiguous memory) for a new backing store is available, V8 crashes with an out-of-memory error.