# How we made a 2D renderer 2-4x faster than the most popular one

[spryte](https://github.com/debuffjp/spryte) is the renderer inside debuff, now a standalone MIT package. On [2dbench](https://github.com/debuffjp/2dbench) in desktop Chrome it draws moving sprites **2-4x faster than PixiJS 8**, the most widely used 2D renderer on the web, and ships about **9x less JavaScript** at startup (10.1 kB vs 87.8 kB, brotli).

![Most sprites each library holds at 60 fps, by scene: spryte ahead in every one](/charts/2026-11-11/max-at-60fps.svg)

2dbench is open: anyone can run it and [submit their device](https://debuffjp.github.io/2dbench/mobile.html). Every library draws pixel-identical frames before anything is timed, and every library's setup is documented in the results. If you can make PixiJS faster, send a pull request: we'll publish the new numbers either way.

No single trick got it there. Tuning the hot path couldn't close a 2x gap; it took a structural change, found through a chain of measurements that each ruled something in or out. Here's that chain, from the first profile to the 8-byte instance format that made the difference.

## The starting point

spryte started as the WebGL2 renderer in `@debuffjp/core`. It was already an instanced, multi-texture batcher: every sprite is one instance in a GPU buffer, up to 8 textures are bound per draw call, and nothing allocates in the hot path. On a bunnymark it held about 100,000 sprites at 60 fps on our test laptop. That was about 2x PixiJS's `Sprite`, but **slower than PixiJS's `ParticleContainer`**.

The question was why, and whether we could do better without turning spryte into a different kind of library.

## 1. Measure where the time goes

Our benchmark already split each frame into two numbers: main-thread time before the GPU sync, and total time to GPU completion. At 200,000 sprites:

| | Frame | Main thread | Per sprite (CPU) |
| --- | --- | --- | --- |
| spryte `draw.sprite` | 21 ms | 14 ms | ~70 ns |
| PixiJS `ParticleContainer` | 13 ms | 7 ms | ~36 ns |

GPU time was about the same. **The gap was JavaScript, per sprite.**

## 2. Profile, don't guess

Our first guess was a micro-optimisation. `draw.sprite` checked `"image" in frame` on every call, and on a DOM image that walks a long prototype chain, so we cached the last image. **The benchmark didn't move.**

So we took a CPU profile at 200k sprites:

| Function | Share of samples |
| --- | --- |
| `Draw.sprite` | 26% |
| `WebGL2Backend.quad` | 23% |
| GPU sync (`readPixels`) | 21% |

The cost was spread across the per-call work itself: reading options, the transform maths, and 13 buffer writes per sprite. No single line was slow, so trimming the per-call path couldn't close a 2x gap. **The fix had to be structural.**

## 3. Stop paying JavaScript per sprite

`ParticleContainer` wins because it's a bulk API. So we added one in spryte's immediate-mode style:

```ts
draw.sprites(frame, positions, count, options);
```

It takes one frame and many positions. The shared transform is worked out once, and each copy is written in a tight loop. That brought spryte level with `ParticleContainer` up to about 200,000 sprites.

Then, at 400,000, spryte fell behind again (28 vs 18 ms). This time the main-thread number was fine, and the **GPU-side** share had grown.

## 4. Then the bottleneck was bandwidth {#_4-the-real-bottleneck-was-bandwidth}

![The whiteboard at the time: 400k at 28 vs 18 ms; main thread fine; texture binds, draw calls and fill rate crossed off; upload size underlined; 52 bytes per sprite times 400k; "only pos changes per frame? 8B, rest as uniforms"](/research/spryte-bandwidth-whiteboard.webp)

*The whiteboard at the time. Every suspect but one crossed off.*

Every copy still uploaded a full 52-byte instance (a 2x3 matrix, a UV rectangle, colour, flash and a texture unit), even though only the position changed. At 400,000 sprites that's about 21 MB per frame.

So we added a second GPU program in which each instance is just a `vec2` position: **8 bytes instead of 52**. Everything shared (the transform, UVs, tint, pixel snapping) became a uniform, sent once per call. And because the instance data is just positions, a `Float32Array` you already have can go straight to `bufferData`:

```ts
const positions = new Float32Array(2 * MAX); // x, y pairs you update each frame
draw.sprites(spark, positions, live);         // no per-copy JavaScript in spryte at all
```

The result: 200,000 sprites in under 3 ms, and 60 fps held at **800,000** sprites, against 400,000 for `ParticleContainer`.

## 5. Particles change per particle {#_5-real-particles-change-per-particle}

Particles in a game vary in scale, rotation and colour, and `ParticleContainer` supports that. We added optional per-copy arrays without giving up the zero-per-copy-JavaScript property:

```ts
draw.sprites(spark, positions, live, { tint: "#ffd36b" }, { scale, rotation, color });
```

- Each array is a separate GPU buffer, uploaded as is.
- Colour stays packed as `0xRRGGBBAA` in a `Uint32Array` and is **unpacked in the shader** with bit shifts, so JavaScript never reorders bytes.
- Arrays you don't pass read constant vertex-attribute defaults (scale 1, rotation 0, white), so you only upload what you use.
- The vertex shader runs exactly the same transform maths as `draw.sprite`.

## 6. Prove it draws the same thing

A fast path that draws slightly different pixels is a bug. We wrote checks that run on a real GPU (headless Chrome): draw a scene the simple way (`sprite()` in a loop) and the fast way, then compare every pixel. Each check was confirmed to fail on a deliberately broken implementation.

The checks found a bug. With per-copy rotation, a copy whose rotation happened to be zero wasn't pixel-snapped, but `sprite()` would have snapped it. At first the 25-pixel difference looked like float noise inside a tolerance, but it was identical on WebGL2 and on Canvas 2D. Float noise wouldn't be. That meant a deterministic cause, which we traced and fixed. The tolerance went back to zero.

## 7. A benchmark that's hard to game

The first comparison was a single run per renderer. To make the numbers hold up, we rebuilt the benchmark:

- **Interleaved rounds.** Several rounds, with the renderer order rotated each round, reported as median with min-max.
- **A pixel-parity gate.** Before any timing, every renderer draws the same seeded frame at 2,000 and at 50,000 sprites. The run fails if a renderer's drawn area differs from the reference by more than 0.5%, or if pixels differ *inside* sprites. "Inside" means the 3x3 neighbourhood is one flat colour in both frames, so edges, where libraries legitimately round differently, don't count. A wrong tint, alpha or texture does. We validated the metric: giving PixiJS a deliberately wrong tint raised it from 0% to 22%.
- **Automatic environment capture,** including power source and plan. We had typed "default power plan" into a note by hand; the laptop was actually on Turbo. The runner now detects it.
- **Like for like:** `draw.sprite` against `Sprite`, and `draw.sprites` against `ParticleContainer`. Both use nearest filtering, no antialiasing and whole-pixel rounding (PixiJS with `roundPixels: true`).

## 8. Trying to break it {#_8-adversarial-review}

Before publishing, we ran three review passes aimed at breaking the result.

**Bias.** The review made sure every property in the dynamic scenes (scale, tint, alpha, rotation) changes over time, so no library can skip work by caching. spryte leads by **4.2x** on dynamic particles and **3.5x** on mixed sprites under those rules. It also checked that the PixiJS setup is its fastest: its WebGPU renderer is slower than its WebGL one here, `roundPixels` costs nothing, and an atlas makes no meaningful difference.

**Robustness.** Six edge cases were found and fixed in 0.1.1, each with a test: a shader compile failure, an oversized `count`, Canvas 2D smoothing after a clip, oversized render targets, a `NaN` count, and drawing after `dispose()`. A new check forces a real GPU context loss and confirms the next frame is pixel-identical to a fresh renderer's.

**Claims.** Every number on this site traces to a committed result file or build output, and the size harness is in the repo.

## The result

![Frame time by particle count: spryte stays under the 60 fps line far longer](/charts/2026-10-20/particles.svg)

Same machine, same assets, same simulation, 3-7 interleaved rounds in 2dbench, parity gate passed, spryte 0.2.1:

| Workload | spryte | PixiJS 8 | spryte is |
| --- | --- | --- | --- |
| Dynamic particles (everything changes per particle), 200k | **5.8 ms** | 24.2 ms (`ParticleContainer`) | **4.2x faster** |
| Sprites with changing rotation, scale, tint and alpha, 4 textures, 50k | **7.8 ms** | 27.1 ms (`Sprite`) | **3.5x faster** |
| Bunnymark, one call per sprite, 50k | **4.7 ms** | 12.2 ms (`Sprite`) | **2.6x faster** |
| Bunnymark, bulk, 200k | **2.8 ms** | 7.2 ms (`ParticleContainer`) | **2.6x faster** |
| JS at startup | **10.1 kB** | 87.8 kB | **~9x smaller** |

*NVIDIA RTX 5060 Laptop GPU, AMD Ryzen 7 260, Chrome 155, AC power, power plan Turbo. Raw data, method and caveats: [benchmarks](/benchmarks).*

## What this doesn't show yet

- **More devices.** Beyond this laptop, 2dbench now has a Linux RTX 4080 laptop, a Windows laptop on AMD integrated graphics, iPhones in Safari and Chrome, and an Android phone in Firefox. spryte's one-call-per-sprite path is ahead on every one: 2.7-3.8x on dynamic particles on desktops and iPhones, 1.6x on the Android phone. On iPhones the bulk paths finish level, because the GPU's fill rate, not JavaScript, is the limit there. See [results across devices](https://github.com/debuffjp/2dbench#across-devices).
- **Other workloads.** Tilemaps, text-heavy UI, clipping, render targets and shaders, object churn, memory and load time aren't measured yet.
- **Other renderers.** PixiJS is the reference everyone knows, but it isn't the only one.

That last point is why we built **[2dbench](https://github.com/debuffjp/2dbench)**, an open benchmark where any 2D web renderer plugs in through a small adapter: Phaser, KAPLAY, Excalibur, LittleJS and three.js are next. It has:
- one shared, seeded simulation, so every library does identical work;
- the same parity gate, with PixiJS `Sprite`, the incumbent, as the reference rather than our own code;
- statistics chosen from a variance study rather than asserted;
- results recorded with their full environment.

Anyone can run it on a phone at [debuffjp.github.io/2dbench/mobile.html](https://debuffjp.github.io/2dbench/mobile.html) and submit the results through a GitHub issue, which a workflow turns into a validated pull request. **If you have a device that isn't in the [community results](https://debuffjp.github.io/2dbench/results.html) yet, please run it.**

## Lessons

1. **Profile before optimising.** The cost was spread thinly, so only a structural change could move it.
2. **Find the bottleneck class.** Per-call JavaScript, then upload bandwidth. Neither yields to micro-tuning.
3. **Make fast paths prove themselves.** Compare pixels against the simple path, on a real GPU, with checks you've seen fail.
4. **Make the benchmark hard to game.** Shared simulation, a pixel gate, every property changing, and the method published with every number.

Try it: `npm install spryte`. The [API reference](/api/spryte/) and the [benchmark method](/benchmarks) are on this site, and the code is at [github.com/debuffjp/spryte](https://github.com/debuffjp/spryte).