Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 6

One test, two engines

Dictionary mode in V8

Theme
Try itOne test, two engines
The table holds the book’s timings for V8 and JavaScriptCore on one machine; your browser’s engine fills the third column. Then add an object literal with the same 32 fields and see what happens to V8’s dictionary mode.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

What is dictionary mode in V8?

V8 stores an object’s properties in one of two ways. In fast mode the object has a hidden class, a map that records which field lies at which offset, and inline caches read a field by its offset. In dictionary mode the map no longer describes the fields: the properties live in a hash table inside the object, and every read looks the name up there. V8 uses it to save work on objects whose shape keeps changing, for example after delete or when many properties are added under computed names. Reading from a dictionary is noticeably slower.

In 2022 Bun appeared, a runtime meant to replace Node, built on JavaScriptCore, the engine of Safari, and server-side JavaScript stopped meaning V8 by default. From the outside the engines behave alike, because the specification requires it. Inside, each makes its own decisions, and programmers do not see them until they measure.

The table comes from the book: one script, shapes2.js, run on Node 24 and on the jsc that ships with macOS, on the same machine. The button runs the same script in your browser on 32,768 objects, scales the time to a million and fills the third column. After that you can add one more mode, an object literal with the same 32 fields.

On small objects JavaScriptCore was a little faster in every mode: one shape in 0.59–0.64 ms against 0.91–0.94 ms for V8, four shapes in 0.88–0.93 against 1.38–1.47, thirty-two in 1.92–2.14 against 3.15–3.39. Objects that got 32 fields under computed names V8 moves to dictionary mode, and a pass took it 42–52 ms, while JavaScriptCore spent 1.25–1.34 ms, about thirty-five times less. An object literal with the same 32 fields V8 keeps in fast mode.

The specification says nothing about speed, so what counts as fast is decided by the engine teams, by the benchmarks they chose themselves. Code written to one engine’s habits can turn out tens of times slower in another, and no documentation of the language says so. The third column shows how your browser decided.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 6

The Engine Wars

Next to this experiment in the chapter
Responsibility for speed is smeared across three parties, and none of them sees the whole picture. The programmer does not know which tricks will work in their user’s engine. The engine team does not know what code will be written tomorrow and judges it by benchmarks it chose itself. And the benchmarks reflect their authors’ ideas of what matters. For systems built on language models, which are trained and checked on task suites, the same question, whether we are measuring the wrong thing, is already about correctness, and speed is secondary there.