Googles Solution: Hidden Classes

By Jeff Cogswell  |  Posted 2008-09-17 Print this article Print


Hidden Classes in V8

Google drew on its team members' expertise with earlier compiled, dynamic languages, such as an experimental language called Self (created when the team members worked at Sun Microsystems). To a programmer like myself, the concept at first sounds horrible. When I first read about it I was shocked that they would do something so seemingly inefficient. But in fact, it works beautifully.

Here's how it works. In JavaScript, objects are initially created with no members. You could create two new objects and neither will have any members. Then your code can add members. You might have a line of code that creates an object; then the next line adds on a new member called Width; then the next line adds a member called Height.

During this time, your object went through three stages: First it had no members, then it had only one member, then it had two members.

Now, typically such member-adding code will take place in a constructor function. This is a special named function in JavaScript that runs when you create a new object. The function gets a name, and that name is in turn used for the class of the object. Thus, you might have a function called Rectangle. Its code will have a line to add a Width member, and then a line to add a Height member. And this is where the V8 smarts of Google Chrome come in.

While compiling the code, V8 will create in the background a compiled class (just like you have in C++ at run-time) that contains no members. Then as the compiler discovers the line of code that adds a Width member, the compiler will create a second class, this one with the Width member. Then as the compiler encounters the next line, it will create a third class, this one with the Width and Height members.

That's right, it creates three classes. But the compiler is smart, because later on in your code, when you create several instances of your Rectangle class, the compiler will know to use the third class.

Of course, that's sort of a simple example. In fact, you might add the eWEEK member to one of your objects outside of the constructor. In that case, the compiler will create a fourth class, this one including the eWEEK member.

In other words, you might have multiple instances of Rectangle, and behind the scenes several of them may share the third class, while others might have their own separate class. These are called hidden classes.

And, of course, the rest of your code is compiled to machine code; the compiler doesn't just make a bunch of classes. That's where the efficiency actually comes in. The idea of generating all these classes is only a minor tradeoff in the long run, because ultimately your code is running as native machine code and is far, far faster than traditional, interpreted JavaScript.

Jeff Cogswell is the author of Designing Highly Useable Software ( among other books and is the owner/operator of CogsMedia Training and Consulting.Currently Jeff is a senior editor with Ziff Davis Enterprise. Prior to joining Ziff, he spent about 15 years as a software engineer, working on Windows and Unix systems, mastering C++, PHP, and ASP.NET development. He has written over a dozen books.

Submit a Comment

Loading Comments...
Manage your Newsletters: Login   Register My Newsletters

Rocket Fuel