Back to Field notes

All field notes

Engineering

Simulating 200 Shoppers at 60 FPS in HTML5 Canvas

Games / 5 min read /

In short

Run the simulation on a fixed timestep of 1/60 second, and draw once per animation frame whatever the clock says.

Keep every shopper in a typed array and reuse each slot when a shopper leaves, so the loop makes almost no garbage.

At 160 to 200 shoppers the work is tiny: about 5 to 7 microseconds per step, against a frame budget of 16.7 milliseconds.

Last verified: December 2026. Numbers below were measured by the studio on 6 December 2026.

The corner shop price-war simulator runs 160 animated shoppers, one rival shop and a profit chart in a single canvas element, with no engine and no imported assets. It is a small version of the real-time simulation work behind our closed browser playtest for an unannounced city-block business tycoon. This note shows how the loop is built and what it costs.

What is the performance budget?

At 60 frames a second each frame has 16.7 milliseconds. The browser needs part of that for layout and painting, so the page script should stay far below it. These are the budgets we set and what we measured.

Budget itemTargetMeasuredHow it is kept
Frame budgetUnder 16.7 ms a frame, script well below halfMedian 0.2 ms, 95th percentile 0.5 ms, worst 3.6 ms for step plus draw, at 160 shoppersPlain loops over arrays, one canvas, no layout work in the frame
Fixed timestepSame result at any frame rateOne step is 1/60 second; a long frame is capped at 0.25 sAn accumulator runs whole steps and keeps the remainder
Typed arrays and pooled slotsNo new objects per shopper per stepSimulation step: 4.9 microseconds at 160 shoppers, 7.2 at 200 (Node, median of 5 runs of 20,000 steps)Float32Array and Uint8Array for state; a leaving shopper is reset in place
Canvas batchingFew draw callsNot needed yet: 160 dots drawn one by one stayed inside the frame numbers aboveIf the crowd grew, group dots by colour and fill one path per colour

Measured on the studio's Windows development machine: the step cost in Node 26, the frame cost in headless Microsoft Edge with the page at 1440 by 900. The frame interval in that run was about 17.7 ms, set by the browser display clock. Your device will differ, so treat these as a guide and measure your own.

How do you build a fixed-timestep loop?

A loop that moves things by the time since the last frame gives different results on a 60 Hz phone and a 144 Hz monitor, and it breaks when a tab wakes up after a long pause. A fixed timestep fixes both: the simulation always advances in equal steps, and the frame time only decides how many steps to run.

const STEP = 1 / 60;       // seconds of simulated time per step
const MAX_FRAME = 0.25;    // never catch up more than this in one frame

function advance(sim: Sim, frameSeconds: number, speed: number): number {
  if (speed <= 0) return 0;
  sim.acc += Math.min(MAX_FRAME, Math.max(0, frameSeconds)) * speed;
  let steps = 0;
  while (sim.acc >= STEP) {
    stepSim(sim, STEP);
    sim.acc -= STEP;
    steps++;
  }
  return steps;
}

let last = 0;
function frame(now: number): void {
  if (document.hidden) { last = 0; return; }   // paused: no work, no catch-up later
  if (last) advance(sim, (now - last) / 1000, speed);
  last = now;
  draw();
  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);

Three details matter. The cap on a frame stops a slow tab from running dozens of steps at once. Resetting last when the tab is hidden stops the clock jumping when it returns. And running at 2x only scales the time fed in, so the model is the same at every speed. That also makes the model testable: the same seed and the same settings give the same profit, and our unit tests check it.

How do you avoid garbage collection pauses?

A garbage collection pause shows up as a dropped frame. The cure is to make less garbage: allocate the state once, then only write into it.

interface Crowd {
  n: number;
  x: Float32Array; y: Float32Array;     // position
  tx: Float32Array; ty: Float32Array;   // where this shopper is walking to
  leg: Uint8Array;                      // 0 arriving, 1 to a door, 2 inside, 3 leaving
}

function createCrowd(n: number): Crowd {
  return {
    n,
    x: new Float32Array(n), y: new Float32Array(n),
    tx: new Float32Array(n), ty: new Float32Array(n),
    leg: new Uint8Array(n),
  };
}

// A shopper who leaves is not deleted: the same slot is reset and becomes the next arrival.
function respawn(c: Crowd, i: number, rand: () => number): void {
  c.x[i] = 0;
  c.y[i] = 160 + rand() * 80;
  c.tx[i] = 200;
  c.ty[i] = 200;
  c.leg[i] = 0;
}

This is object pooling in its simplest form: the pool is the array, and a slot is either in use or ready to be reset. Our simulation still creates a few small values each time a shopper respawns, so it is not allocation free, but nothing grows with the number of steps and we saw no long frames in our run.

When does canvas batching matter?

Every call to fill or stroke has a cost. For a few hundred dots you can ignore it, which is what the simulator does. If the crowd grew to thousands, group dots by colour and fill each colour once.

function drawDots(ctx: CanvasRenderingContext2D, c: Crowd, colours: string[], colourOf: Uint8Array): void {
  for (let k = 0; k < colours.length; k++) {
    ctx.fillStyle = colours[k];
    ctx.beginPath();
    for (let i = 0; i < c.n; i++) {
      if (colourOf[i] !== k) continue;
      ctx.moveTo(c.x[i] + 3, c.y[i]);
      ctx.arc(c.x[i], c.y[i], 3, 0, Math.PI * 2);
    }
    ctx.fill();   // one fill per colour, not one per dot
  }
}

Measure before you do this. At our size it would add code for no visible gain.

See it running

The price-war simulator is free and runs in your browser: set a price, pick a stock tier and try to beat the rival shop in 60 seconds. If you would like to see where this kind of simulation ends up, you can join the closed playtest list for the unannounced city-block tycoon, or read the first look field note.

Edited

Frequently asked questions

Why use a fixed timestep for a canvas simulation?

It makes the simulation behave the same at any frame rate and after a slow frame, and it makes the model repeatable, so a test can check that the same seed gives the same result.

How many shoppers can the simulator draw at 60 FPS?

The page runs 160 shoppers. In our measurement the simulation step cost 4.9 microseconds at 160 shoppers and 7.2 at 200, and a whole frame of step plus draw took a median of 0.2 milliseconds in Edge. We did not test above 200.

Do you need a game engine for this?

Not at this size. A requestAnimationFrame loop, a few typed arrays and the 2D canvas API were enough, with no imported assets.

Sources

  1. Red Center Studio measurements of the price-war simulator: step cost in Node and frame callback time in Edge, 6 December 2026, 6 Dec 2026
  2. Window: requestAnimationFrame() method, MDN Web Docs
  3. Fix Your Timestep, Gaffer On Games

More field notes