docs/heap/objects-and-maps.md
This document describes how JavaScript objects are represented in V8's heap and how "Maps" (also known as hidden classes or shapes) are used to describe their structure.
All standard JavaScript objects in V8 are represented by the JSObject class (or its subclasses). A JSObject typically has the following memory layout:
Map object that describes the structure of this object.EmptyFixedArray (if no properties).Smi representing the identity hash code.PropertyArray for fast properties.NameDictionary (or SwissNameDictionary) for slow (dictionary mode) properties.GlobalDictionary for properties of global objects.FixedArray for fast elements.NumberDictionary for slow elements.JSObject instance itself, after the header, for faster access.Every HeapObject in V8 has a pointer to a Map as its first field. The Map is crucial for performance as it enables Inline Caching (IC) for property access.
A Map contains:
JS_OBJECT_TYPE, JS_ARRAY_TYPE, STRING_TYPE).DescriptorArray that describes the names and locations of the object's properties.(Based on src/objects/map.h)
instance_size (Byte)inobject_properties_start_or_constructor_function_index (Byte)used_or_unused_instance_size_in_words (Byte)visitor_id (Byte)instance_type (Short)bit_field, bit_field2, bit_field3 (Bytes/Int)prototype (TaggedPointer)constructor_or_back_pointer_or_native_context (TaggedPointer)instance_descriptors (TaggedPointer)dependent_code (TaggedPointer)prototype_validity_cell (TaggedPointer)transitions or prototype_info (TaggedPointer)V8 uses three different modes to store named properties, balancing access speed and modification flexibility:
JSObject memory layout itself.PropertyArray pointed to by the object.DescriptorArray attached to the Map to find its index in the PropertyArray.PropertyArray can grow independently of the object size.NameDictionary or SwissNameDictionary).SwissNameDictionary is an alternative representation that is currently disabled by default and controlled by the v8_enable_swiss_name_dictionary GN argument.Map and DescriptorArray. This avoids creating a massive number of maps for highly dynamic objects.To avoid duplicating the implementation of array methods (like slice, map, etc.) for all 20+ different elements kinds, V8 uses a C++ template pattern called CRTP (Curiously Recurring Template Pattern) in ElementsAccessor (see src/objects/elements.cc).
When a new property is added to an object in fast mode, V8 creates a new map for the object. The old map has a "transition" pointing to the new map for that specific property name. This forms a transition tree, allowing V8 to share maps among objects with the same structure.