---
title: "Chapter 1. An Accidental Revolution"
url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch01"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18T12:24:57.879Z"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
access: "free"
alternates:
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch01"
    markdown: "https://www.mikefluff.com/ru/read/from-closures-to-autonomous-systems/ch01.md"
  - locale: "en"
    url: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch01"
    markdown: "https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch01.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch01"
    markdown: "https://www.mikefluff.com/pt/read/from-closures-to-autonomous-systems/ch01.md"
---

# Chapter 1. An Accidental Revolution

*[JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)*

In May 1995 Brendan Eich wrote, in ten days, the prototype of a language that people inside Netscape called Mocha. The story is told so often that only one moral survives: the language was made in a hurry, which is why `typeof null` still answers `"object"` and an empty array added to an empty object turns into a string. The moral is true, it just explains very little. Dozens of languages were thrown together in a hurry in the nineties, and nearly all of them died along with the products they were thrown together for. Eich’s prototype was luckier: thirteen years later entire teams at Google, Mozilla and Apple would spend years building compilers around it. So something got into the language in those ten days of May that is not visible at first glance.

Eich was hired by Netscape in early 1995 on the promise that he would embed Scheme in the browser, and he started work on the third of April. Scheme never made it into the browser. In those same weeks Netscape was negotiating with Sun about Java, and Marc Andreessen and Bill Joy decided that a second language on the page had a right to exist only as a little brother. The little brother was supposed to resemble the big one with curly braces and semicolons. Resembling BASIC was forbidden by a separate clause, because BASIC belonged to Microsoft, and that year Netscape was dividing the whole internet with Microsoft. It got no classes either: they take a long time to build, and with classes the little brother would have started competing with the big one. Eich made the syntax exactly as ordered and put inside what he himself liked. Functions could be stored in variables and passed around like any other value, which he borrowed from Scheme. Objects did without classes, which he borrowed from a language called Self.

In September 1995 the language shipped in a beta of Navigator 2.0 under the name LiveScript, and in December Netscape and Sun announced it in a joint press release as JavaScript. According to Eich, Andreessen had been steering towards that name from the day he came up with the working title «Mocha». The name guaranteed confusion at job interviews for twenty years ahead, although by then the two languages had little in common: braces, semicolons and a press release.

* * *

Self was invented by David Ungar and Randall Smith in 1986 at Xerox PARC and later developed at Stanford and at Sun. It has no classes at all. You get a new object by copying an old one, and whatever an object cannot do itself, it delegates to a parent it holds a reference to. Ungar and Smith considered classes needless bureaucracy: why describe in advance what an object will be when you can take a ready one and adjust it. Programmers love that kind of freedom. Whoever has to run the program fast hates it. A C++ compiler knows an object’s class, which means it knows that field `x` lies, say, eight bytes from the start, and reads it with a single machine instruction. A Self compiler knows nothing of the kind, because every object is laid out however it pleases, so the field has to be looked up by name in a dictionary on every access, and if it is not there, in the parent’s dictionary.

The Self team spent the late eighties beating this problem, and beat it with two techniques that this story will come back to. The first was described in 1989 by Craig Chambers, Ungar and Elgin Lee. The machine itself notices that many objects have the same set of fields in the same order and quietly creates a shared map for them that records which field lies at which offset. The programmer knows nothing about maps and cannot refer to them. The language abolished classes, and its implementation quietly brings them back by watching how the program actually treats its objects. The second technique is called an inline cache. The line of code that says, for example, `o.x` remembers the map of the last object that passed through it and the offset where the field was found. If the next object arrives with the same map, there is no need to search a dictionary. In 1991 Urs Hölzle, together with Chambers and Ungar, taught such a cache to remember several maps at once and noticed that it had a side effect more valuable than its main job. The cache also keeps a diary of which objects actually pass through every line of the program, and all an optimizing compiler needs is to read that diary.

In 1994 Hölzle and Lars Bak, both from the Self team, founded with a few colleagues a company known as Animorphic and built a fast virtual machine for Strongtalk, a Smalltalk dialect, on these ideas. In 1997 Sun bought Animorphic, and their machine became the basis of HotSpot, on which Java runs to this day.

From Self, JavaScript in 1995 took the rules of the game: prototypes, delegation, objects that change shape on the fly. It did not take the technique that made all this fast. Ten days were not enough for it, and Mocha’s tasks were modest anyway: check a form before submitting it, swap a picture when the mouse hovers over it. Eich’s prototype compiled the program to bytecode, and the bytecode interpreter, in the author’s own words, came out simple and slow and looked up object properties by name, the way Self did before maps were invented. For thirteen years that was enough.

Self came to JavaScript a second time in September 2008, together with the Chrome browser. Its engine, V8, was written by a small team in Aarhus, Denmark, led by the same Lars Bak, who by then had worked on the virtual machines of Self, Smalltalk and Java. In its very first documentation V8 attributed its speed to fast property access through hidden classes, machine code with inline caches and precise garbage collection. V8’s hidden classes are Self’s maps under a new name. The engine’s documentation refers to Self explicitly, and in the V8 source they are still called `Map`.

The freedom the language handed to programmers in 1995 was paid for in 2008 with technique from the same lab, and the person who did it was one of its authors. For all thirteen years in between, the web considered JavaScript a language for highlighting buttons, and in its own way it was right. As long as browsers had no fast engine, nobody in their right mind would have written a program longer than a couple of hundred lines in it.

* * *

What this freedom means for the engine can be seen in two short fragments. The first could have been written in 1997, when JavaScript 1.2 allowed functions to be declared inside functions. Before that, all functions lived at the top level of a script, and a language designed after Scheme spent two years without closures. The fragment has no `let`, arrow functions or classes, because they had not been invented yet:

```
function makeCounter() {
  var n = 0;
  return {
    inc: function () { return ++n; },
    get: function () { return n; }
  };
}

var c = makeCounter();
c.inc(); c.inc();
c.get();   // 2
c.n;       // undefined: n cannot be reached from outside
```

There is no class, constructor or `private` keyword here, yet the object has private state, and the only way to reach it is through the two functions handed out. Both of them captured the same variable `n`, and the garbage collector will not touch it while at least one of the functions is alive, even though `makeCounter` finished long ago. Private fields with `#` entered the standard only in 2022, and for a quarter of a century before that, data in JavaScript was hidden exactly this way, with closures. The same mechanism would later grow into modules, event handlers and all the asynchrony that the third part of this book is about.

In the second fragment there is nothing for the programmer to look at:

```
const a = { x: 1, y: 2 };
const b = { y: 2, x: 1 };
```

To a human, `a` and `b` are the same: the same fields, the same values. To V8 they are objects of different hidden classes, because their fields appeared in a different order, and a function used to the first will stumble over the second. How much it hurts can be measured. The directory `experiments/01-hidden-classes` contains a script that reads field `x` a million times in the same loop and changes only the number of differently shaped objects passing through that line. All objects are small, two fields each, so that size does not muddy the picture. On Node 24 and a laptop with an Apple M4 Max, a pass takes about 1.1 ms when there is one shape. With four shapes it is about 1.4 ms, and the cache still copes. With thirty-two it is about 3.3 ms: V8 stops remembering shapes one by one and looks the field up by a more general and slower path. Three times slower out of nowhere, although the programmer only changed the order in which objects acquired their fields.

The first version of this experiment showed me a twentyfold difference, and I nearly wrote that the cache was to blame. The culprit turned out to be something else. When an object gets its twentieth field through computed names like `o[’p’ + j]`, V8 gives up on a hidden class for it and keeps its fields in an ordinary dictionary, the way Self did before maps. The same loop over such objects takes 35–42 ms, more than thirty times longer than over small ones. JavaScriptCore, Apple’s engine built into macOS, runs the same code in 1.3 ms, because it does not do this. How differently engines solve the same problems is the subject of the sixth chapter.

If you run the same script with the `–trace-deopt` flag, the engine will tell you itself what happened when a function compiled for one shape received another:

```
[bailout (kind: deopt-eager, reason: wrong map): begin.
 deoptimizing ... <JSFunction getX ...>, <Code MAGLEV> ...]
```

`wrong map`, translated into human language, means the bet did not pay off. The engine looked at the program and decided that objects of one shape always pass through this line. 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.

* * *

The first edition of the ECMA-262 specification came out in June 1997, when Netscape and Microsoft had already diverged in their implementations so far that an arbiter was needed. The specification says not a word about the DOM, `setTimeout` or the network. It describes values, objects, functions, the order of property lookup along the prototype chain and the order of evaluating expressions, and everything else is brought to the language by the house it lives in. The browser brings the document, timers and the event loop described in the HTML standard. Node in 2009 would bring files, sockets and its own event loop on libuv. A language with no input and output of its own turned out to be a convenient tenant that could be housed anywhere. Ryan Dahl, who housed it on the server, later explained his choice like this. JavaScript is single-threaded, browser programmers had long been used to non-blocking requests through AJAX, and the server-side JavaScript projects of the time blocked the thread on every disk or network access, and in a language without threads such a server simply stopped answering everyone else for the duration of each access. When the third part reaches Meteor and its fibers, it will turn out that most of the trouble there was born on this boundary between the language and its house.

Take the dullest expression in the language, `o.x`. Whether the object has a field `x` is decided by the author of the program, and if they forgot to create it, they get `undefined` without a single warning. Where to look for the field, first in the object itself and then along the prototype chain until the chain ends, is written in the specification. Everything else the engine decides on its own without asking anyone: where the field lies in memory, what machine code reads it, how many times to check the object’s shape. On top of that, it revises these decisions on the fly. And when an object dies is not decided by the author at all; from day one that has been the garbage collector’s job.

Every chapter will end with an analysis like this. I call it the responsibility boundary: on one side lies what the programmer must say themselves, on the other what the machine will work out for them without breaking what was promised. It is too early to call it a theory; it is a working tool, and in the last chapter I will look separately at where it helps and where it starts bending the facts to fit. One thing is visible already. When the boundary moves towards the engine, responsibility passes to whoever checks it. A failed bet by an optimizing compiler costs time and never correctness, because deoptimization returns the program to the behaviour the specification prescribes, and that is why the engine can be allowed to bet as much as it likes. Later in the book, executors will appear for whom a failed bet costs correctness. They have no such safety net, and it has to be built around them by hand.
