← Back to JavaScript, page 5

javascript-0099 · Hard senior

What are hidden classes (shapes) in V8, and how do they affect the performance of object property access?

Engines such as V8 give each object a hidden class, also called a shape or map, that describes its property names, their order, and where they are stored. Objects created with the same properties in the same order share one shape, which lets inline caches make property reads fast. Adding properties in different orders, deleting properties, or changing shapes at hot call sites makes code slower, so initialize objects consistently.

Full answer

JavaScript objects are dynamic: you can add and remove properties at any time. A naive implementation would store each object as a hash table, which is slow. To avoid that, engines such as V8 (Chrome and Node.js) create hidden classes. A hidden class, called a map in V8 and a shape in other engines, records which properties an object has, in which order, and at which offset each value is stored.

Objects that get the same properties in the same order share one hidden class. When you add a property, the object moves to a new hidden class through a transition from the old one. The engine keeps a tree of these transitions. So { a, b } built as a then b and another object built as b then a end up with different hidden classes, although they have the same keys.

Why it matters: inline caching. At a property access site such as point.x, the engine remembers the hidden class it saw and the offset of x. The next time, it checks the class and reads at the offset, with no lookup. A site that sees one shape is monomorphic and fast. With a few shapes it is polymorphic and somewhat slower. With many shapes it is megamorphic and falls back to slower generic lookup. Optimizing compilers also rely on stable shapes to generate tight machine code.

Practical rules:

  • Initialize all properties in the constructor or object literal, in the same order, and do not add them later.
  • Do not use delete on hot objects. Deleting a property can move the object to dictionary mode, which is a slow hash table. Set it to null or undefined instead, or use a Map for objects used as dictionaries.
  • Keep the type of each field stable: do not switch a number field to a string.
  • Do not pass objects of many different shapes through the same hot function.
  • Arrays have similar kinds: keep them holey-free and of one element type when you can.

Do not change code for this unless a profile shows property access as a hot spot. These are engine details that change between versions, and other engines do similar things with different names. The snippet uses V8 test functions that are enabled by a Node.js flag. They are for learning and debugging, not for production code.

javascript
// The % functions are V8 debugging helpers. They need the flag --allow-natives-syntax
// and are not standard JavaScript. Use them only to learn, never in production code.
function Point(x, y) {
  this.x = x;
  this.y = y;
}
const p1 = new Point(1, 2);
const p2 = new Point(3, 4);
console.log('same constructor, same shape:', %HaveSameMap(p1, p2));

const a = { x: 1, y: 2 };
const b = { y: 2, x: 1 };
const c = { x: 10, y: 20 };
console.log('literal with same order shares a shape:', %HaveSameMap(a, c));
console.log('literal with another property order:', %HaveSameMap(a, b));

const grown = { x: 1 };
grown.y = 2;
console.log('same keys, built step by step vs literal:', %HaveSameMap(grown, a));

const user = { id: 1, name: 'Asha', email: 'a@example.com' };
console.log('fast properties before delete:', %HasFastProperties(user));
delete user.id;
console.log('fast properties after delete:', %HasFastProperties(user));

const dictionary = {};
for (let i = 0; i < 2000; i++) dictionary['key' + i] = i;
console.log('object with 2000 added keys has fast properties:', %HasFastProperties(dictionary));

Run with Node.js v24.22.1 using the --allow-natives-syntax flag as js-99.js; two runs gave identical output. Output: same constructor, same shape: true literal with same order shares a shape: true literal with another property order: false same keys, built step by step vs literal: false fast properties before delete: true fast properties after delete: false object with 2000 added keys has fast properties: false

Key points

  • A hidden class (map or shape) stores the property names, order, and offsets of an object.
  • Same properties in the same order share a shape; adding a property moves to a new shape.
  • Inline caches remember the shape at a site, so monomorphic access is fast and megamorphic access is slow.
  • Initialize all fields in one order, avoid delete, and keep field types and shapes stable.
  • Verify with a profile before changing code; these are version-specific engine details.

Common mistakes

  • Adding properties to objects in different orders in different code paths.
  • Using delete on objects that are read in hot loops.
  • Mixing value types in one field, such as numbers and strings.
  • Rewriting working code for shapes without a profile that shows a problem.

Follow-ups

  • What are monomorphic, polymorphic, and megamorphic call sites?
  • What is dictionary mode and why is it slower?
  • Why do class constructors that initialize all fields help the engine?

Tips

  • Define shape, transition, and inline cache in plain words first.
  • Give three rules: same order, no delete, stable types.
  • Say that you would profile before changing code, and that details depend on the engine version.