This tutorial walks through how V8 builds and uses Hidden Classes (Maps in V8's C++ codebase) from the ground up, showing how dynamic JavaScript objects gain the memory layout and property-access speed of compiled C++ structs, and how PropertyConstness::kConst enables aggressive constant folding in Maglev and TurboFan.
(For the low-level bitfields of DescriptorArray, TransitionArray, LoadHandler, and StubCache, see Hidden Classes and ICs and Descriptor and Transition Arrays.)
In a statically typed language like C++ or Java, a class's field layout is fixed at compile time:
struct Point { int x; int y; } stores x at byte offset 0 and y at byte offset 4.p->y compiles to a single machine load instruction (mov eax, [rdi + 4]).JavaScript has no static struct declarations, and properties can be added to any object on the fly. Without hidden classes, every object would have to store its properties in a hash table (dictionary), turning every point.y read into a string hash computation, bucket probe, and key comparison.
V8 avoids dictionary lookups by observing that most JavaScript objects are initialized in predictable, repeatable patterns. Every HeapObject in V8 starts with a pointer to a Map (its hidden class). The Map acts as a blueprint describing:
DescriptorArray).PropertyArray) each property lives at.Map to transition to when a new property is added (TransitionsAccessor / TransitionArray).Consider this constructor function:
function Peak(name, height, extra) { this.name = name; this.height = height; if (extra) { this.prominence = extra; } } const m1 = new Peak("Matterhorn", 4478); const m2 = new Peak("Mont Blanc", 4810, 4695);
this (Map0)When new Peak("Matterhorn", 4478) begins, V8 allocates a fresh JSObject with slack space for in-object properties (this.name, this.height, this.prominence). Initially, it points to Peak's initial hidden class, Map0, which has 0 descriptors.
this.name = name (Map0 -> Map1)When this.name = "Matterhorn" executes:
Map0's transitions and creates Map1.Map1's DescriptorArray records that "name" is stored at in-object field index 0 (Representation::HeapObject(), FieldType::Class(String)).Map0 records an inline weak transition (TransitionsAccessor::kWeakRef) to Map1: Map0 --("name")--> Map1.this.height = height (Map1 -> Map2)When this.height = 4478 executes:
Map1 to Map2.Map1's DescriptorArray, Map1 and Map2 share the same DescriptorArray, with Map1->NumberOfOwnDescriptors() == 1 and Map2->NumberOfOwnDescriptors() == 2!"height" at in-object field index 1 (Representation::Smi()).extra is undefined, m1 finishes construction with Map2.m2 (Map0 -> Map1 -> Map2 -> Map3)When new Peak("Mont Blanc", 4810, 4695) runs:
Map0, follows the existing "name" transition to Map1, and follows the existing "height" transition to Map2 -- allocating zero new Maps for the first two properties!extra is truthy (4695), this.prominence = 4695 executes on Map2, creating Map3 (which appends "prominence" at in-object field index 2 to the shared DescriptorArray using pre-allocated descriptor slack).What if another function adds a different property after height?
const m3 = new Peak("K2", 8611); m3.country = "Pakistan/China";
Now Map2 has two outgoing transitions: one for "prominence" (Map3) and one for "country" (Map4):
Map2's raw_transitions() upgrades from an inline single weak pointer (kWeakRef) to a TransitionArray (kFullTransitionArray) containing two sorted [Key, Weak<Map>] entries: ["country" -> Map4, "prominence" -> Map3].Map3 already claimed index 2 of the original DescriptorArray for "prominence", Map4 cannot overwrite index 2 with "country". Therefore, Map4 allocates its own DescriptorArray (["name", "height", "country"]).Because transitions depend on the exact sequence of property additions, creating { a: 1, b: 2 } and { b: 2, a: 1 } dynamically walks two different paths in the transition tree (Map0 --"a"--> MapA --"b"--> MapAB vs Map0 --"b"--> MapB --"a"--> MapBA), producing two distinct Maps where a and b sit at swapped field offsets. Initializing properties in a consistent order ensures all instances share a single Map and keeps Inline Caches monomorphic.
PropertyConstness::kConst and Compiler Constant FoldingEvery descriptor in a DescriptorArray tracks a 1-bit PropertyConstness flag (src/objects/property-details.h):
PropertyConstness::kConst: The property has been initialized once (when transitioning to the Map) and has never been overwritten with a different value on any instance of this Map.PropertyConstness::kMutable: The property has been overwritten at least once after initialization.kConst Powers Code Generation and DeoptimizationConsider a top-level configuration object or prototype method holder:
const config = { scale: 10 }; function compute(x) { return x * config.scale; }
config is created, its Map marks the "scale" descriptor as PropertyConstness::kConst (Representation::Smi()).compute(x) becomes hot and is compiled by Maglev or TurboFan:config is a constant object reference, checks its Map, and inspects the "scale" descriptor in config->map()->instance_descriptors().PropertyConstness::kConst, the compiler reads config.scale (10) at compile time, registers this compiled Code object in the Map's DependentCode under DependentCode::kFieldConstGroup, and compiles compute(x) directly to return x * 10 -- completely eliminating the property load from config!; Conceptual x64 optimized assembly for compute(x) with kConst folding: ; No load from `config` at all! imul eax, edi, 10 jo deopt_exit_overflow
config.scale = 20?StoreIC / LookupIterator::PrepareForDataProperty.MapUpdater sees that "scale" is currently PropertyConstness::kConst and the new value (20) differs from the existing value (10).MapUpdater updates the descriptor in-place to PropertyConstness::kMutable and invokes DependentCode::DeoptimizeDependencyGroups(isolate, map, DependentCode::kFieldConstGroup).compute's optimized Code is immediately marked for lazy deoptimization! The next call to compute falls back to Ignition (and if re-optimized later, compiles a normal field load instead of constant-folding 10).d8You can inspect Maps, DescriptorArrays, and TransitionArrays directly using a debug build of d8 with --allow-natives-syntax:
// Run with: out/x64.debug/d8 --allow-natives-syntax function Peak(name, height) { this.name = name; this.height = height; } const m1 = new Peak("Matterhorn", 4478); %DebugPrint(m1);
In the %DebugPrint(m1) output, look for:
map: 0x... <Map[24](HOLEY_ELEMENTS)>instance descriptors #2: 0x... <DescriptorArray[2]> showing (const data field 0:s, p: 0, Representation: HeapObject) and (const data field 1:s, p: 1, Representation: Smi).back pointer: 0x... <Map> linking back up the transition chain to Map1 and Map0.