Sub topic 13
Array Elements Kinds and Fast Arrays
This chapter assumes you have read the earlier chapters on hidden classes and type feedback. You should also be able to run Node.js with flags. A JavaScript array can hold any mix of values and can have gaps in it. Most arrays in real programs do neither, and V8 makes those common arrays faster by recording what kind of values each array holds.
The working model
Every array in V8 has an elements kind. An elements kind is a label that describes the array's indexed values, which V8 stores separately from its named properties. The elements kind is recorded in the array's hidden class (its map). This means two arrays with different elements kinds have different maps.
These six kinds cover most code:
| Elements kind | Values it allows | Storage |
|---|---|---|
| `PACKED_SMI_ELEMENTS` | Small integers only | Tagged integers, no tag checks needed for arithmetic |
| `PACKED_DOUBLE_ELEMENTS` | Any numbers | Raw unboxed 64-bit floats |
| `PACKED_ELEMENTS` | Any values | Tagged pointers |
| `HOLEY_SMI_ELEMENTS` | Small integers, with holes | As above, plus hole checks |
| `HOLEY_DOUBLE_ELEMENTS` | Numbers, with holes | As above, plus hole checks |
| `HOLEY_ELEMENTS` | Anything, with holes | As above, plus hole checks |
"Packed" means every index from 0 to length - 1 holds a value. "Holey" means at least one index is a hole, which is a missing element and not the same as undefined.
Transitions only go one way
Elements kinds form a lattice. An array can move toward a more general kind, but it never moves back, even if you later remove the value that caused the change.
The following script prints the map of an array after each change, so you can watch the kind move. The kind appears in the map and elements lines of the %DebugPrint output.
// Run with: node --allow-natives-syntax kinds.js
const arr = [1, 2, 3];
%DebugPrint(arr); // PACKED_SMI_ELEMENTS
arr.push(4.5);
%DebugPrint(arr); // PACKED_DOUBLE_ELEMENTS
arr.push('x');
%DebugPrint(arr); // PACKED_ELEMENTS
arr[10] = 1;
%DebugPrint(arr); // HOLEY_ELEMENTS: indexes 5 through 9 are holes
arr.length = 5;
%DebugPrint(arr); // still HOLEY_ELEMENTS: transitions never reverseWhy holes cost more
When V8 reads a hole, it cannot just return undefined. JavaScript requires the engine to look up the prototype chain, because Array.prototype[7] could exist. Optimized code for holey arrays therefore needs extra checks, or it must rely on a guarantee that the prototypes have no indexed properties. Packed arrays skip all of this.
Common errors
- Preallocating with
new Array(n). This creates a holey array right away. If you are filling it in order, start with[]and callpush. - Reading past the end. A loop like
for (let i = 0; i <= arr.length; i++)readsarr[arr.length]. That read misses and goes to the prototype chain lookup, and the extra miss can make the access site less specialized. - Sneaking in doubles.
-0,NaN,Infinity, and1.5all move a SMI array to doubles. - Mixing kinds at one call site. Arrays with different kinds have different maps, so a function that receives both sees more than one shape. This pushes its inline caches toward polymorphic.
Exercise
You write const a = [1, 2, 3]; a.push(2.5); a.pop();. What elements kind does a have now, and why?
Show answer
PACKEDDOUBLEELEMENTS. Pushing 2.5 moved the array from SMI to DOUBLE. Popping the value afterwards does not reverse the transition, because elements kinds only move toward more general kinds.
Exercise
Why is new Array(1000) followed by a loop that fills every index slower for later reads than building the array with push?
Show answer
new Array(1000) creates a HOLEY array, and filling every slot does not make it packed again. Later reads still pay for hole checks, while an array built with push stays PACKED.