
How dynamic array access work? - mohanpierce
In C if i declare int x[4]<p>Which tells me its an integer array with each array element size fixed of size 4.<p>So after initialization when I access x[3] : address of array +element size which is 2 * index =&gt; (x[3]=2000+2*3) Assuming 2000 is the array address<p>In Javascript<p>We have<p>var a=[3,&quot;rohan&quot;,6] &#x2F;&#x2F;Dynamically typed<p>Here array elements are not equally sized for eg.a[2] is a string whose element size is different than a[0] which is an integer<p>So how does the array access work?
======
obl
The simple naive way is to indirect through a pointer, so your array elements
would each be say 8 bytes pointing to things of various types.

In practice you can play a lot of tricks to avoid the overhead.

One example would be NaN-tagging. Another would be to speculatively store the
array elements "unboxed" as long as they all have the same type and fallback
to pointers when the array actually becomes heterogeneous.

I'm not up to date on what today's javascript JIT do for storage optimizations
but when you add in sparse arrays etc, I'm sure it's quite involved.

