Skip to content Skip to sidebar Skip to footer

Hidden Classes And Equivalence Between {} Object Vs. Custom Constructor (v8)

Given this article: http://richardartoul.github.io/jekyll/update/2015/04/26/hidden-classes.html If you create a constructor such as: function Point(x,y) { this.x = x; this.y =

Solution 1:

As the article says in its summary:

Always instantiate your object properties in the same order so that hidden classes, and subsequently optimized code, can be shared.

In your alternative example var obj1 = { a: 5, b: 10 }; var obj2 = { b: 10, a: 5 }, the properties are clearly not added in the same order, so the hidden classes cannot be shared.

You don't have to use a constructor per se, but using a constructor is the easiest way to ensure that you're getting the performance you want. (It is also generally considered ergonomic and good coding practice.)

Post a Comment for "Hidden Classes And Equivalence Between {} Object Vs. Custom Constructor (v8)"