Sub topic 17
Beyond V8: SpiderMonkey and JavaScriptCore
This chapter assumes you have read the earlier chapters on tiered compilation, hidden classes, and inline caches. You should be comfortable with V8's tiers (Ignition, Sparkplug, Maglev, TurboFan) and with the idea that objects have shapes. Everything so far has used V8. Firefox runs on SpiderMonkey and Safari runs on JavaScriptCore (JSC). Both engines rely on the same core ideas, but they use different names and split their tiers in different places.
Same Ideas, Different Names
All three engines give objects a hidden layout descriptor, cache property lookups at each access site, and collect type feedback to drive speculative optimization. Each engine has its own name for these pieces.
| Concept | V8 | SpiderMonkey | JavaScriptCore |
|---|---|---|---|
| Hidden class | Map | Shape | Structure |
| First tier | Ignition (bytecode 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) | DFG JIT |
| Top-tier optimizing JIT | TurboFan | Warp (Ion backend) | FTL JIT |
| Inline cache format | Feedback vector plus IC handlers | CacheIR | Polymorphic inline caches, watchpoints |
SpiderMonkey's Tiers
SpiderMonkey starts code in a simple C++ interpreter. Warm code moves to the Baseline Interpreter and then to the Baseline Compiler, which emits machine code that mirrors the bytecode. Hot code is optimized by Warp, which feeds the Ion compiler backend.
SpiderMonkey's inline caches are built on CacheIR, a small intermediate language. Each cache stub is a short CacheIR program, for example "check that the shape is S, then load slot 2". The same CacheIR programs are shared by the baseline tiers and by Warp. Warp translates the CacheIR collected at run time directly into optimized code. In SpiderMonkey, the inline cache data is the type feedback.
JavaScriptCore's Four Tiers
JSC has four tiers. The LLInt interprets bytecode and is written in a portable assembly-like language called offlineasm. The Baseline JIT removes interpreter dispatch overhead. The DFG (Data Flow Graph) JIT performs speculative optimization with a moderate compile cost. The FTL (Faster Than Light) JIT runs the heaviest optimizations through JSC's B3 backend.
JSC relies heavily on watchpoints. A watchpoint is a registered assumption, such as "no object with this structure has had its prototype changed". Optimized code can skip a runtime check and depend on the watchpoint instead. If something breaks the assumption, the watchpoint fires and the dependent code is jettisoned, which is JSC's term for deoptimization.
What This Means for Your Code
The advice from earlier chapters carries across engines. Stable object shapes, consistent property order, monomorphic access sites, and arrays that hold one kind of value help all three engines. The exact thresholds differ: how many shapes make a site megamorphic, when a function gets promoted to the next tier, and which deletes push an object into dictionary mode.
Exercise
A colleague says that because Firefox has no equivalent of Maglev, shape-stable code matters less there. Is this right?
Show answer
No. Shape stability affects the inline caches in every SpiderMonkey tier. Warp builds its optimized code from the CacheIR those caches record, so polymorphic or megamorphic sites still produce slower optimized code. The number of tiers changes how quickly code warms up. It does not change why stable shapes help.
Exercise
What is the JavaScriptCore term for V8's map, and what JSC mechanism lets optimized code skip some shape checks?
Show answer
A structure. Watchpoints let optimized code skip some checks: they register assumptions, and when an assumption is violated they fire and the dependent code is jettisoned.