Chapter 18Down the Rabbit Hole: How V8 Makes Dynamic Objects Fast, and Why It Keeps Getting Hacked
{x: 1, y: 2} and {y: 2, x: 1}: same keys, same values, interchangeable to the language. To V8 (Chrome, Node.js, Deno, Electron, Cloudflare Workers) they are two different kinds of object, and a function that sees both gets measurably slower.
That's the tip of a ~35-year-old mechanism that also sits behind a long line of serious Chrome security bugs. I came out thinking of the JIT as a bet-placing machine whose mistakes are security holes.
Objects are dictionaries#
o.x means: look up "x" in the object's property table, else climb the prototype chain. Objects grow and shrink freely, so the naive version is a hash lookup per access with every object carrying its own key names. That's tens of nanoseconds and several dependent loads, billions of times. Self, where this idea comes from, measured a simple "Cartesian point" at 10 machine words without the trick below and 3 with it. V8 wants o.x to cost roughly one load.
Trick one: hidden classes#
Most objects are built the same way, so give each layout a shared descriptor. V8 calls it a Map (SpiderMonkey: Shape; JavaScriptCore: Structure). It records which properties exist, in what order, at which offsets, in what representation.
Maps form a transition tree. Each property you add follows an edge labelled with its name, in insertion order:
M0 ──x──▶ M1 ──y──▶ M2 {x: 1, y: 2}
M0 ──y──▶ M1' ──x─▶ M3 {y: 2, x: 1}That answers the opening puzzle. With pointer compression, each tagged value is 4 bytes, and an object with Map M2 looks like this (my derivation; %DebugPrint shows the real thing):
offset 0 map ─▶ M2 offset 4 properties ─▶ (overflow store, empty) offset 8 elements ─▶ (array elements, empty) offset 12 x Smi(1) offset 16 y Smi(2)
Know the Map and you know x is at offset 12. No search.
Trick two: inline caches#
A sticky note at every property read. In function getX(o) { return o.x; }, the o.x bytecode has a slot in the function's feedback vector. First call: the slot is empty, V8 does the real lookup, finds x at in-object index 0 of M2, and stores (M2, handler). The handler is a small integer packing "field, in-object, this index". Second call: load the Map, compare with M2, decode the handler, load the field. With one Map seen, the site is monomorphic.
The idea goes back to Smalltalk inline caching and Hölzle, Chambers and Ungar's polymorphic inline caches (ECOOP '91): a median 11% speedup on their Self programs, 27% when the type feedback also drove recompilation.
How much does a shape cost?#
A small Node script times one read site with different numbers of shapes. One run, Node v24.12, no CPU pinning: noisy.
| Shapes at the site | ns/call |
|---|---|
| 1 (monomorphic) | 0.99 |
| 4 (polymorphic) | 4.06 |
| 8 (megamorphic) | 4.90 |
| 64 (megamorphic) | 4.80 |
The cliff surprised me: mono to poly, about 4x. Megamorphic adds little on top because the global stub cache hits. The loop includes an index and array load, so these aren't pure property-access cost.
Two more effects from the same script
delete flips an object into dictionary mode, with no stable Map to cache on, while setting the property to undefined keeps it fast. Arrays have their own one-way lattice of elements kinds: push 1.5 onto an array of small integers and its Map changes to a double-elements kind, the backing store is rewritten, and it never goes back.
Trick three: betting on the note#
When getX gets hot, the optimizing tiers (Maglev, then TurboFan) read the feedback slot and compile code that assumes it:
CheckMaps o, {M2} ; deopt if o.map != M2
v = LoadField o, offset=12A compare, a conditional jump and a load. The code is registered as dependent on M2, so if M2 is deprecated it's thrown away.
getX(o): CheckMaps o, {M2}
LoadField o, offset=12
IC: MONO (M2)When is a bet worth it? (expected cost)
Let t_f be the guarded fast path, t_s the generic path, C_d the amortized cost of a deopt and re-tier, and p the chance the guard fails:
E = (1 - p) * t_f + p * (t_s + C_d)
speculate only if p < (t_s - t_f) / (t_s - t_f + C_d)With a fast load near 1 ns and a deopt somewhere in the microseconds (an estimate, not a measurement), p has to be on the order of 1 in 1,000 to 1 in 10,000. That's why V8 waits for stable feedback, and it works because programs are far more regular than the language allows.
Deoptimization is what makes the bet legal: without a way back to generic code, optimized code would have to be correct for every input.
Where it breaks: the guard you removed#
If the optimizer can prove a Map can't have changed between two points, it removes the redundant CheckMaps. That proof depends on a model of which operations have side effects. If the model is wrong, compiled code keeps using the old layout after the object changed: type confusion, from which attackers build addrof and fakeobj, then arbitrary memory read and write.
- CVE-2020-6418. Receiver-Map inference special-cased
JSCreateand forgot to mark the result unreliable, though it can run user code (e.g. aProxyviaReflect.construct). That switched an array from small-integer to double elements mid-function while inlinedArray.popstill treated the store as tagged. - CVE-2020-16009. V8 decided certain Maps couldn't be deprecated, installed no code dependency, and they were deprecated anyway. An in-place field generalization led to double/tagged confusion.
- CVE-2025-2135. Map inference for one IR node lacked an aliasing check and returned "reliable maps" when two nodes referred to the same runtime object: object-array versus double-array confusion.
Same function family, five years apart. The code deciding which guards are safe to remove is, in effect, a soundness proof written in C++.
V8's response is containment: the V8 sandbox (pointer-compression cage plus indirection tables) limits what a corrupted heap can reach, but doesn't fix the logic bugs. The 2025 bug was exploited within the sandbox.
The open question#
Can speculative JIT optimizations be verified at production scale? TurboTV (ICSE 2024) validates TurboFan optimizations with SMT solvers; OptFuzz (USENIX Security 2024) and DUMPLING (NDSS 2025) fuzz optimization paths and engine state. They find bugs. None yet gates a Chrome release.
Never removing a CheckMaps gives up much of the speed the machinery exists for. So the question isn't whether we can make it safe; it's safe and fast, in a codebase that keeps changing under the proof.
The boring takeaway: initialize fields in the same order, don't delete, keep arrays homogeneous, keep hot sites under five shapes. I now read that as "don't make the bettor lose".
Sources#
- Hölzle, Chambers, Ungar, Optimizing Dynamically-Typed Object-Oriented Languages With Polymorphic Inline Caches, ECOOP 1991
- V8 docs and blog: hidden classes, fast properties, elements kinds, Maglev, sandbox
- V8 source: stub-cache.h, handler-configuration.h
- Project Zero, CVE-2020-6418 root cause and CVE-2020-16009 root cause
- Zellic, Pwning V8CTF (CVE-2025-2135)
- TurboTV: Translation Validation for JIT Compiler in V8, ICSE 2024
- OptFuzz, USENIX Security 2024
- DUMPLING, NDSS 2025
- Mathias Bynens, JavaScript engine fundamentals: Shapes and Inline Caches
- Vyacheslav Egorov, What's up with monomorphism?