Course contentsShow
← JavaScript
Lesson 1908 of 2,72340. PerformanceFree lesson

Object Shape and Property Order

Objects with the same properties added in the same order share a hidden class.

Object Shape and Property Order

You'll learn: How adding properties in a consistent order helps JavaScript engines run your code faster.

Same Shape, Same Speed

In the previous lesson, you learned that V8 creates hidden classes behind the scenes to optimize object property access. Here's the key insight: when two objects have the same properties added in the same order, they share the same hidden class — and that means faster execution.

Think of it like a form template. If everyone fills out fields in the same order (name, then email, then age), you can process all forms using one efficient system. But if some people write age first, then name, then email, you need different systems for each variation — even though the information is technically the same.

Order Matters

// These two objects have DIFFERENT hidden classes
const user1 = {};
user1.name = 'Alice';
user1.age = 30;

const user2 = {};
user2.age = 25;      // age FIRST
user2.name = 'Bob';  // name SECOND

// These two objects SHARE the same hidden class
const user3 = {};
user3.name = 'Charlie';
user3.age = 35;

Even though user1 and user2 end up with the same properties, they don't share a hidden class because the properties were added in different orders. But user1 and user3 do share one because they followed the same pattern.

The Performance Implication

When objects share a hidden class, V8 can reuse the same optimized code paths. When they don't, the engine must handle more variations, which slows down property access across your application.

Key Takeaway: Create objects with the same properties in the same order to let V8 share hidden classes and optimize property access efficiently.