What Are Hidden Classes?
You'll learn: How V8 optimizes JavaScript objects under the hood using internal structures called hidden classes.
The Core Idea
JavaScript objects can have properties added or removed at any time — they're incredibly flexible. But this flexibility is slow for the engine to process. To solve this, V8 (Chrome's JavaScript engine) creates hidden classes (also called "shapes" or "maps") that describe the structure of your objects.
Think of it like a blueprint: when you create an object with specific properties in a specific order, V8 generates an internal blueprint. If you create another object with the exact same properties in the same order, V8 reuses that blueprint. This allows the engine to optimize property access — it knows exactly where in memory each property lives.
Why This Matters
Objects that share the same hidden class can be processed much faster. The engine can predict memory layout and generate highly optimized machine code.
However, if you add properties in different orders or add/delete properties dynamically, you create different hidden classes. This forces the engine to de-optimize or create multiple blueprints, slowing things down.
// These two objects share the same hidden class
const obj1 = { x: 10, y: 20 };
const obj2 = { x: 15, y: 25 };
// This object has a DIFFERENT hidden class (different order)
const obj3 = { y: 20, x: 10 };
Adding properties after creation can also "transition" the object to a new hidden class, which has performance costs if done repeatedly.
Key Takeaway: Hidden classes are V8's internal blueprints for object structures. Objects with identical property names and order share the same hidden class, enabling faster property access and better optimization.