Performance Monitor
Measures frame rate, slow frames, how long the game’s own code takes each frame, and memory, and checks them against the project’s performance budget.
Files: performance-monitor.js (the component, no dependencies) and performance-monitor.test.mjs (5 tests).
Copy performance-monitor.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createPerfMonitor } from "./performance-monitor.js";
const perf = createPerfMonitor({ budget: { fps: 55, p95Ms: 20, workMs: 8, memoryMB: 150 } });
function frame(t) {
perf.frame(t); // once per frame with the requestAnimationFrame time
perf.begin(); update(); draw(); perf.end();
}
perf.draw(ctx, 8, 8); // small graph, e.g. only when debugging
window.__perf = perf; // so tests can read perf.check()
API
createPerfMonitor({ budget, samples, now, longFrameMs }) → perfperf.frame(time?) / perf.begin() / perf.end()perf.stats() → { fps, avgMs, p95Ms, maxMs, longFrames, workMs, workP95Ms, memoryMB, samples }perf.check() → { status: "PASS" | "FAIL" | "NOT TESTED", fails: [...], stats }NOT TESTED until enough frames (default 60) were measuredperf.reset() / perf.draw(ctx, x, y)perf.quality → "high" | "low": drops to "low" after 3 s under budget, so the game can cut effects (e.g. particles scale)
Memory only shows in Chromium browsers (performance.memory); elsewhere memoryMB is null and the memory check is skipped.
Frames longer than 250 ms (a tab in the background) are left out.
Engines
| Engine | Notes |
|---|---|
| Vanilla, Phaser, PixiJS, Three.js | yes. Phaser: call frame() in the scene’s update. Pixi: app.ticker.add(() => perf.frame()). Three.js: in renderer.setAnimationLoop. |
Tests
Node tests with a fake clock: fps, p95 and long frames, work time, background gaps ignored, budget pass and fail, not tested with too few frames, quality drop.
Run them with node --test toolkit/components/performance-monitor/ (Node 20 or newer, nothing to install).