JavaScript · Senior · Question 26 of 75
Why can adding properties to objects in different orders slow code down in V8?
- ✓Correct answer: The objects get different hidden classes and optimize worse
- 2V8 sorts properties alphabetically on every object access
- 3Property order doesn't affect engine performance at all
Explanation
The engine optimizes property access by the object's "shape" (hidden class / shape). If objects of one type are created with different orders or sets of fields, the call site sees many shapes and loses the fast path. Initialize all fields in the constructor right away and don't remove them with delete.