Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 1
Object shapes in your browser
Hidden classes and inline caches in JavaScript
Everything is computed in your browser; nothing is sent anywhere.
About this experiment
What are hidden classes in JavaScript?
A hidden class is an internal description of an object that the engine creates on its own: which fields the object has, in what order they arrived and at what offset each one lies. V8 calls hidden classes maps; other engines call them shapes or structures. Objects that got the same fields in the same order share one map, so { x: 1, y: 2 } and { y: 2, x: 1 } are different to V8. An inline cache at each place in the code that reads a field remembers the maps of the objects that passed and the field’s offset. While there are few maps the field is read at once; when there are many, the engine looks it up by a slower, general route.
Self, the language JavaScript took its classless objects from, solved its speed problem in the late 1980s with two tricks. The machine notices objects with the same set of fields and quietly gives them a shared map, and each line like o.x remembers the map of the last object through it and the offset where the field was found. In 2008 the same tricks came to JavaScript with V8, where the maps were renamed hidden classes. The programmer cannot see them or refer to them.
The top of the experiment is the chapter’s own example: two objects with the same fields and values. Swap the order of b’s fields and the diagram shows b going down another branch of transitions to a map of its own. Below it runs the book’s script shapes.js. “Run the measurement” builds 32,768 objects in each of four modes, gives a fresh copy of the loop ten warm-up passes and times how long it takes to read x from all of them. The time is scaled to a million objects and set next to the book’s.
In the book, on Node 24 and an Apple M4 Max, the median of 50 passes over a million objects was 1.06–1.23 ms for one shape, 1.37–1.41 ms for four and 3.08–3.31 ms for thirty-two, where V8 stops remembering shapes one by one. Objects that got 32 fields by computed names V8 moves to dictionary mode, and the same loop took 34.5–42.2 ms. JavaScriptCore ran that code in 1.3 ms. Your numbers will differ, and since dictionary mode at the twentieth field is V8’s own decision, in Safari the last gap is far smaller.
This is where the chapter’s argument starts. The freedom to change an object’s shape on the fly reached programmers in 1995, and the speed to go with it came thirteen years later, once the engine learned to guess shapes on the programmer’s behalf. It guesses silently: two objects that look identical to a person are different to the engine, and the price of that difference shows only when you time it.
From the book
JavaScript: From Closures to Autonomous Systems · Chapter 1
An Accidental Revolution
… It compiled machine code for that decision with a check at the entrance. The check failed, and the code was thrown away on the spot. This is called deoptimization. The program noticed nothing and returned the correct answer, only a little later. The second part of the book is devoted entirely to this cycle: observe, bet, check, roll back. It is one of the most productive techniques in the history of dynamic languages, and in the last part I will try to show that systems trusted with things far more serious than choosing machine instructions are built on the same scheme today.