Box2D in WebAssembly
15 Nov 2026
- Box2D v3 now runs as WebAssembly on the server and in the page. On the ball pit with the CPU slowed 10 times, the frame rate went from 43.1 to 59.1 fps.
- Physics in the page fell from 402 to 135 ms a second, and the page’s results matched the server bit for bit over 4,000 ticks.
- Balls resting in a pile now sleep. A tick of the Sandbox sandpit fell from 0.338 to 0.115 ms.
For client prediction, ship one engine to both ends as WebAssembly, and test that both ends match bit for bit, including bodies falling asleep and waking.
We compiled one physics engine to WebAssembly and run it on the server and in every browser with bit-identical results, so the page’s prediction replays exactly the server’s world. This fits any game with client prediction on a C or C++ engine. We moved from planck.js, a JavaScript Box2D port, to Box2D v3, and planck is now gone from every bundle. Current state: Physics.
Measured how: the benchmark script ran before and after the change on an Apple M3 Max. “10x” means Chrome’s CPU slowed 10 times. The “Chromebook profile” is that slowdown at 1366 by 768, with Auto quality and the GPU on. Numbers are medians of 3 runs. Browser rows were measured again on a quiet machine, alternating before and after.
Why
The server was fine, at about 1.4 ms of work a tick, but the browser was not. On the ball pit at 10x, frames dropped to 40 a second, and about 45% of the time was physics, because each update rewinds the page’s world and replays it. The physics numbers came from driving a ball in the classic game and converting what we saw (10 Nov).
Same answers
- A test steps both engines tick by tick, and acceleration, top speed and wall bounces match to 0.0001 tiles. Each map moved over once its features were ported.
- Every body stays bit-identical between client and server over 4,000 ticks, including piles that sleep and wake at different ticks.
- Gravity piles at first differed on 360 of 4,000 ticks, and the exactness test found four causes. Gravity and damping ran inside the step, after the collision. Contacts between two resting bodies had no warm start. Touching contacts were not all recoloured from current state, so the two sides solved a pile in different orders, with impulses 10% apart. And a moved sleeping body kept its old contact manifolds.
Making it cheaper
- The engine read the clock about 50 times a step for an unused profile, each read a call out of WebAssembly. Removing it saved about 7% of a ball pit step at 10x and half a step on a plain map.
- Pit balls at rest now sleep. Awake contacts per step fell from 229 to 8 on Lab: Big and 277 to 9 on Hangout. A Node tick fell from 1.055 to 0.858 ms on Lab: Big, 0.424 to 0.255 ms on Hangout and 0.338 to 0.115 ms on the Sandbox sandpit. On the Chromebook profile, Lab: Big went from 21 to 42 fps and Hangout from 34 to 39.
- A moving body now grows its box in the broadphase tree in place instead of being removed and re-inserted, which had been 15% of a Lab: Big prediction tick. A tick fell from 0.567 to 0.307 ms on Lab: Gravity, 0.267 to 0.154 on Hangout, 0.572 to 0.455 on Lab: Big and 0.618 to 0.597 on the gravity sandpit.
- The contact solver handles four contacts at once, one per SIMD lane. When all four have one point, as every circle contact does, it now skips the second point’s terms. Results stay bit-identical on seven maps, and a loaded gravity sandpit tick fell from 0.343 to 0.316 ms. On the Chromebook profile the four pit levels stayed at 53 to 59 fps. Physics on the two gravity pits fell from 0.6 to 0.8 ms a frame to 0.1 ms, and frames over 33 ms from 3 to 0 a run.
| What | Before | After | Measured |
|---|
| Ball pit frame rate, 10x | 43.1 fps | 59.1 fps | Quiet, alternating |
| Ball pit slowest frames at p99, 10x | 50 ms | 16.8 ms | Quiet, alternating |
| Physics in the page, ball pit at 10x | 402 ms/s | 135 ms/s | Quiet, alternating |
| Same, with Light netcode | 402 ms/s | 0 ms/s | Light runs no physics |
| Ball pit, Chromebook profile | 37.2 fps | 59.6 fps | Quiet, alternating |
| Gravity ball pit, Chromebook | 20.1 fps | 55.5 fps | Quiet, alternating |
| Lab: Gravity, Chromebook | 10 fps | 46.2 fps | Quiet, alternating |
| Lab: Layers, Chromebook | 37.4 fps | 58.8 fps | Quiet, alternating |
| Joining a ball pit game, Chromebook, Classic netcode | 6.64 fps | 56.9 fps | Load 4 to 10 |
| Same, Light netcode (called Off before) | 60 fps | 60 fps | Load 4 to 10 |
| Update bytes per player, 12 v 12 | 35,005 B/s | 15,952 B/s | Load 5 to 14 |
| Update lateness at p99, 12 v 12 | 1.75 ms | 2.1 ms | Load 5 to 14 |
| Room tick at p99, ball pit, 12 v 12 | 1.43 ms | 1.27 ms | Load 5 to 14 |
| Game end write, 8 players | 5.01 ms | 4.28 ms | Load 5 to 14 |
| SQL statements per game end | 251 | 234 | Load 5 to 14 |
The same release also brought Light netcode, wire 2 and the drawing and tick work. On the ball pit at the Chromebook profile, physics went from 6.4 to 0.2 ms a frame, and all script from 13 to 6 ms. Drawing stayed at 2.8 to 2.9 ms and is now the largest part. Readings near 10 fps on busy levels right after the release were machine load.
Reuse
- For prediction, ship one engine to both ends as WebAssembly, and test that they match bit for bit over thousands of ticks, including bodies that sleep and wake.
- Look for profiling or timing calls inside the engine, since in WebAssembly each one crosses the boundary.
- Alternate before and after runs on a quiet machine. A loaded machine read 10 fps where the truth was near 60.