Sub topic 17
Beyond V8: SpiderMonkey and JavaScriptCore
Every earlier chapter used V8 as its example. Firefox runs on SpiderMonkey, and Safari, along with every browser on iOS, runs on JavaScriptCore (JSC). This chapter assumes you already know V8's tiers (Ignition, Sparkplug, Maglev, TurboFan), hidden classes, inline caches, type feedback, and deoptimization. It maps those ideas onto the other two engines. It also points out where the engines really differ, so you can tell which performance advice holds everywhere and which is specific to V8.
The same ideas under different names
All three engines rest on one set of ideas. Each starts code in a cheap tier, profiles it, and gives a compiler that speculates on observed types the code that runs often. Each gives objects a shared layout descriptor and uses inline caches keyed on that descriptor. Mostly the names change.
| Concept | V8 (Chrome, Node.js) | SpiderMonkey (Firefox) | JavaScriptCore (Safari) |
|---|---|---|---|
| Object layout descriptor | Map (hidden class) | Shape | Structure |
| First tier | Ignition interpreter | C++ interpreter, then Baseline Interpreter | LLInt (Low Level Interpreter) |
| Fast non-optimizing JIT | Sparkplug | Baseline Compiler | Baseline JIT |
| Mid-tier optimizing JIT | Maglev | (none; Warp covers this) | DFG JIT |
| Top optimizing JIT | TurboFan | WarpMonkey (Warp) with the Ion backend | FTL JIT (B3 backend) |
| Inline cache representation | Feedback vector slots, IC handlers | CacheIR stubs | Structure-keyed access cases, polymorphic stubs |
| Leaving optimized code | Deoptimization | Bailout | OSR exit |
| Array storage categories | Elements kinds | Dense elements and flags | Indexing types |
| Slow property storage | Dictionary mode | Dictionary mode | Uncacheable dictionary structures |
SpiderMonkey: CacheIR as the single source of truth
Tiers
SpiderMonkey starts a function in a generic C++ bytecode interpreter. Code that warms up moves to the Baseline Interpreter. That tier still interprets bytecode, but it uses the same frame layout and inline caches as the Baseline Compiler. The Baseline Compiler, the next step up, emits machine code for each bytecode op with little analysis, much like Sparkplug. Hot code ends up in Warp. Warp builds the optimizing compiler's intermediate representation (MIR) and then uses the Ion backend for register allocation and code generation.
How feedback reaches the optimizer
The most interesting design choice is CacheIR. An inline cache stub in SpiderMonkey is written in a small, linear intermediate language. A property load stub might read "guard that the object's shape is S, then load fixed slot 1." The baseline tiers run or compile these stubs. When Warp optimizes a function, it reads the CacheIR stubs that were attached at each site and transpiles them straight into MIR.
V8 stores type feedback in feedback vectors, and TurboFan or Maglev interpret that feedback separately. In SpiderMonkey, the IC code itself serves as the feedback. The practical effect is consistency: whatever the baseline IC learned is exactly what the optimizer assumes. A shape guard that fails in Warp code triggers a bailout to baseline code, which is SpiderMonkey's word for deoptimization.
Shapes
SpiderMonkey's shapes do the same job as V8 maps. Objects that receive the same properties in the same order share a shape, and adding a property follows a transition to a child shape. Internally, property metadata lives in shared property maps that are separate from the shape. That is a memory optimization and changes nothing about how you should write code. SpiderMonkey also has dictionary-mode objects, and deleting a property or adding a very large number of them can push an object into that mode.
JavaScriptCore: four tiers and watchpoints
Tiers
JSC has four tiers. LLInt is an interpreter written in a portable assembly language called offlineasm, so it starts fast without a JIT. The Baseline JIT emits template code and collects profiling data. DFG (Data Flow Graph) is a mid-tier optimizer that compiles fairly quickly and speculates. FTL (Faster Than Light) is the top tier. It runs heavier optimizations through JSC's B3 compiler backend, which replaced an earlier LLVM backend.
Each tier-up is driven by execution counters, and JSC sets the thresholds so that only a small share of functions ever reach FTL. When a speculation fails, JSC performs an OSR exit (on-stack replacement exit). The exit moves the running frame back to baseline code, as V8's deoptimization does. If a function keeps exiting, JSC recompiles it with less aggressive assumptions.
Structures and butterflies
JSC calls a hidden class a Structure. Each object holds a compact structure ID and a small amount of inline property storage. Extra named properties and indexed elements live in a separately allocated block called the butterfly. It gets that name because named properties grow in one direction from the pointer and indexed elements grow in the other.
Arrays carry an indexing type: Int32, Double, Contiguous (any values), or ArrayStorage (sparse or unusual arrays). Like V8 elements kinds, these move toward more general categories and rarely move back.
Watchpoints
JSC relies heavily on watchpoints. A watchpoint is a registered dependency that invalidates compiled code when a fact changes. One example of such a fact: "no object with this structure has ever transitioned away." The compiled code then skips a structure check, because the engine will throw that code away if the fact stops being true. V8 does something similar with "stable maps" and code dependencies. JSC uses watchpoints in more places, for example for global variables that are never reassigned.
Checking shapes in JSC
V8's %HaveSameMap only works in V8. The jsc shell, built with WebKit and shipped on macOS inside the JavaScriptCore framework, has a describe function that prints an object's structure.
// Run with the jsc shell: jsc shapes.js
function Point(x, y) {
this.x = x;
this.y = y;
}
const a = new Point(1, 2);
const b = new Point(3, 4);
print(describe(a));
print(describe(b));
// Both lines report the same Structure address and StructureID.
b.z = 5;
print(describe(b));
// b now reports a different Structure that also lists z.The exact output format changes between WebKit versions. Compare the Structure address or StructureID field rather than the whole line. For Firefox, the Firefox Profiler shows which tier each sampled frame ran in, which is the most practical way to look inside SpiderMonkey without building its shell.
What transfers and what does not
Advice that holds in every engine
- Initialize all properties in the constructor, in the same order. All three engines build transition trees keyed on insertion order.
- Keep each hot call site to one or a few shapes. All three use inline caches whose performance falls off as the number of shapes grows.
- Keep the value types at a site stable. All three speculate on observed types and pay for leaving optimized code.
- Avoid
deleteon hot objects and holes in hot arrays. All three have slow dictionary paths and general-purpose array storage.
Details that are engine-specific
- Polymorphism limits. The number of shapes an inline cache handles before it goes megamorphic differs by engine and version. Don't tune code to one engine's number.
- Tier thresholds. How many calls or loop iterations trigger optimization varies. A microbenchmark that warms up enough for TurboFan may never reach FTL, or the reverse.
- Specific fast paths. Built-ins such as
Array.prototype.map, spread, orObject.assignare optimized differently in each engine. A trick that is fast in V8 may be neutral or slower in JSC.
Exercise
A teammate reports that a hot function "bails out" in Firefox and has an "OSR exit" in Safari. In V8 terms, what is happening, and what is the most likely first thing to check in the code?
Show answer
Both terms mean deoptimization: optimized code hit a failed speculation and moved execution back to a lower tier. Start by checking whether the function receives values of changing types or objects of different shapes at a hot property access, for example objects built with properties in different orders, or a number argument that is sometimes a string.
Exercise
Why does SpiderMonkey's CacheIR design make baseline ICs and optimized code agree on their assumptions, while V8 gets a similar result by a different route?
Show answer
In SpiderMonkey, Warp transpiles the CacheIR stubs attached at each site directly into its MIR, so the optimizer reuses exactly the guards and loads the IC recorded. V8 records feedback in feedback vectors, and Maglev and TurboFan each read that feedback and build their own speculative code from it. Both approaches base optimization on observed shapes and types. They differ in whether the IC code itself or a separate feedback record is the input.
Exercise
Which of these tips can you rely on across V8, SpiderMonkey, and JavaScriptCore: (1) keep each call site to at most four shapes, (2) assign properties in the constructor in a fixed order, (3) avoid deleting properties on hot objects?
Show answer
Tips 2 and 3 hold everywhere, because all three engines use insertion-order shape transitions and fall back to dictionary-mode storage after deletes. Tip 1 is not portable: polymorphic inline cache limits differ by engine and version, so the general rule is to keep the number of shapes small, not to aim for a specific number.