Are JavaScript Arrays actually implemented as arrays?

In SpiderMonkey, arrays are implemented basically as C arrays of jsvals. These are referred to as “dense arrays”. However, if you start doing un-array-like things to them — like treating them like objects — their implementation is changed to something which very much resembles objects.

Moral of the story: when you want an array, use an array. When you want an object, use an object.

Oh, a jsval is a sort of variadic type which can represent any possible JavaScript value in a 64 bit C type.

Leave a Comment