Hidden Class
Javascript is prototype based inherent but not class based. However, V8 engine does create hidden class in order to optimize performance. The engine reuse hidden class over and over when it is called. Also, hidden class creation is happened at 2 phases during runtime. One is when you create an object by calling contractor call and second when you assign value to property at first time.
function User() {
this.name;
this.age;
}
var user = new User(); // create hidden class, let's call it c0.
user.name = "Kei" // create another one associated with c0. Let's call this c1.
user.age = 1201 // create another one associated with c1
So Why V8 needs these hidden classes? There are 3 reasons for it. It helps..
- Fast Property Access
Dynamic Machine Code Generation
Efficient Garbage Collection
Fast Property Access
It seems creating hidden classes uses memory but they are reused. Two advantages are that hidden classes property access does not require a dictionary lookup and enabling V8 to use the classic class-based optimization (inline caching).
Avoid dictionary lookup take O (log N)
is C++.
- Object function also creates hidden class?
- Prototype chain?
https://github.com/v8/v8/wiki/Design Elements#dynamic-machine-code-generation