18. Hidden Class
■ Javascript
○ Prototype based o0.__proto__.__proto__
○ Change type at runtime
○ => Hard to track object & variables
■ Hidden class
○ Internal representation of the type system
○ => Fast property access
19. function Point(x, y) {
this.x = x;
this.y = y;
}
const p1 = new Point(12, 3);
const p2 = new Point(5, 9);
const p3 = new Point(12);
const hero = { name: 'Superman' };
Javascript Code
24. Hidden Class
const p5 =
new Point(13, 37);
p5.a = 'a';
const p6 =
new Point(13, 37);
p6.b = 'b';
25. Hidden Class
/* C2 */ const p1 = new Point(13, 37);
/* C1 */ const p2 = new Point(2);
/* C0 */ const p3 = new Point();
/* C2 */ const p4 = new Point(4, 2);
26. Optimization tips
■ Avoid using delete
○ Won’t be backed by hidden class
■ Init object in the same order
○ Same hidden classes, same transitions
■ Avoid adding properties after the object
creation
○ If need: Adding in order of props
28. Inline Caching
/* C2 */ const p1 = new Point(13, 37);
/* C1 */ const p2 = new Point(2);
/* C0 */ const p3 = new Point();
/* C2 */ const p4 = new Point(4, 2);
29. Inline Cache
■ Monomorphic
○ single shape
○ fastest possible IC
■ Polymorphic
○ more than one shape
○ linear search among cached entries
■ Megamorphic
○ too many shapes
○ slowest ICs, hitting global cache
30. Optimization tips
■ Prefer Monomorphism than Polymorphism
■ Same method repeatedly over many different method
only once
32. Optimization tips
■ Avoid using delete
■ Init object in the same order
■ Avoid adding properties after the object
creation
■ Prefer Monomorphism than
Polymorphism
■ Same method repeatedly over many
different method only once
33. Digging Deeper
● Deeper in Inline Caching
● Type Feedback, Type Guard
● Intermediate Representation, Deoptimization
● Garbage Collection
Most JavaScript engines use a dictionary-like data structure as storage for object properties - each property access requires a dynamic lookup to resolve the property's location in memory
GC: Pause time: 4.4ms
It’s rather hard to optimize a dynamically typed, prototype-based language, such as JavaScript.
Objects can change their type during runtime and it happens implicitly.
To track types of JavaScript object and variables, V8 introduced the concept of hidden classes.
During runtime V8 creates hidden classes that get attached to each and every object to track its shape/layout.
That allows to optimize the property access time.
Find o0 in allObjects (lookup to resolve the object’s location in memory),
Find a property named "z" in o0 (dynamic lookup to resolve the property’s location in memory) and return its value. (Should we have tried p1.x, we could have stopped here, returning 12.)
Since o0 does not have a property named "z", fetch o0.__proto__ to see if it has a property named "z", otherwise look if o0.__proto__.__proto__ has a property named "z", repeat this process down the prototype chain until __proto__ is null (which means no object prototype was found).
creates a new hidden class everytime the constructor function declares a property