How Firefox Made Calls Between JavaScript and WebAssembly Faster
This article explains how Mozilla optimized the Firefox engine to dramatically speed up function calls between JavaScript and WebAssembly by reducing bookkeeping, eliminating trampolines, merging activations, applying monomorphic call optimizations, and improving built‑in function handling, resulting in call performance that now exceeds native JavaScript‑to‑JavaScript calls.
Mozilla has long aimed to make WebAssembly as fast as possible, designing it from the start for high throughput and using a streaming baseline compiler to reduce load times. A high‑priority goal is to simplify and accelerate the interaction between JavaScript and WebAssembly, whose function calls were historically slower.
In the latest Firefox Beta, JavaScript‑to‑WebAssembly calls are now faster than non‑inlined JavaScript‑to‑JavaScript calls. The article first reviews how JavaScript engines (V8, JavaScriptCore, Chakra, SpiderMonkey) handle function calls using a call stack and stack frames.
Two main optimization strategies are applied: reducing bookkeeping (removing unnecessary work in organizing stack frames) and cutting the medium (eliminating the need to switch between separate activation “folders”). By merging JavaScript and WebAssembly activations into a single activation, the engine avoids costly C++ trampolines.
Further speed gains come from monomorphic (single‑type) calls: when the JavaScript caller always passes arguments of the same type to a WebAssembly function, the JIT can specialize the call, removing boxing/unboxing overhead. Inline caching and type specialization make these calls even faster.
Built‑in functions (e.g., Math.random) also receive a fast path. When a WebAssembly function calls a built‑in implemented in C++, the engine now bypasses the trampoline by using a direct entry stub, dramatically reducing call overhead.
The article concludes that these optimizations bring JavaScript‑WebAssembly interaction in Firefox to parity or better than pure JavaScript calls, and similar improvements are expected in other browsers. Future work includes extending support to more built‑ins and leveraging upcoming WebAssembly GC proposals.
UC Tech Team
We provide high-quality technical articles on client, server, algorithms, testing, data, front-end, and more, including both original and translated content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.