4 ms·
Spoiler: V8's default array implementation holds pointers to boxed objects [Questions 5 & 6], but there are special-case implementations for arrays of doubles [
by bakery2k 9y ago
Spoiler: V8's default array implementation holds pointers to boxed objects [Questions 5 & 6], but there are special-case implementations for arrays of doubles [1] and 16-bit integers [9] (and presumably others). However, adding a single string to an array of doubles violates the special-case and requires every double to be boxed, significantly increasing memory footprint [2].
It's interesting to compare this to a "NaN-boxing" approach, which allows pointers and doubles (and other simple values) to all exist in a single "namespace" of 8-byte values. In the (uncommon?) case of a heterogeneous array, this can save memory by avoiding the need to box simple values. The downside is that NaN-boxing prevents the use of full 64-bit pointers.
- GlitchMr 9y agoThis isn't a special cased array specialization for 16-bit integers (16-bit integers are so rare that special casing those is not worth it). Note that a standard array is not used in [9], rather an `Int16Array` is being used. Due to JavaScript specification requirements (arrays of different integer sizes can share storage), the ONLY sane implementation of Int16Array is well... an array of 16-bit integers.
- deleted 9y ago[deleted]