Make browser games run smoothly at 60 fps on phones and load fast. Measure first (FPS / frame-time overlay, Chrome performance profile, draw calls), then fix the real bottleneck. CPU (allocation and garbage collection, collision checks, AI), rendering (draw calls, overdraw, canvas state changes, devicePixelRatio, shaders, shadows), memory leaks, loading (asset size, lazy loading, compression), ...
Download skill (.zip) Files on GitHubGame performance optimization
Measure, then fix the biggest thing, then measure again. Never optimize blind.
1. Measure
// tiny frame-time overlay: draw last in the HUD
let ft = 16.7, worst = 0, last = performance.now();
function perfTick() { const n = performance.now(), d = n - last; last = n; ft += (d - ft) * 0.05; worst = Math.max(worst * 0.99, d); }
function perfDraw(ctx) { ctx.fillStyle = ft > 18 ? "#f55" : "#5f5"; ctx.font = "12px monospace"; ctx.fillText(`${(1000 / ft).toFixed(0)} fps ${ft.toFixed(1)} ms worst ${worst.toFixed(0)}`, 8, 14); }
- Budget: 16.7 ms per frame at 60 fps. Phones are 3–6× slower than a dev laptop. Aim for < 8 ms on desktop.
- Find the cause: Chrome DevTools → Performance → record 5 s of play. Look at the longest tasks: scripting (your update), rendering/painting, or GC (garbage collection: sawtooth memory, periodic spikes).
- Split timing:
performance.now()around update vs render, shown in the overlay. - three.js:
renderer.info.render.callsand.triangles. Pixi/Phaser: their debug or stats plugins. - Test on the viewer’s real device through the Bash preview, not only the sandbox.
2. Fix by bottleneck
CPU: update is slow
- Collision checks O(n²) → spatial hash or grid (game-architecture patterns). Check only what can collide (bullets vs enemies, not bullets vs bullets).
- AI and pathfinding every frame → every few frames or staggered across entities. Cache paths, use flow fields for crowds (pathfinding).
- Skip work off-screen (sleep far entities).
GC: spikes every few seconds
- No allocation per frame: no
new,{},[], closures,.map/.filteror string building in the hot loop. - Object pools for bullets, particles, popups. Reuse vectors (
tmp.set(x, y)).
Rendering: render is slow
- Canvas 2D: cache static layers in offscreen canvases, cull off-screen, group by style or image, no
shadowBlur/filters per frame (canvas-2d-rendering). - Too many sprites for Canvas 2D (> ~1–2k) → PixiJS / Phaser (WebGL batching).
- WebGL/three.js: instancing, merged static meshes, fewer materials, texture atlases, smaller shadow maps, fewer lights, no post-processing on mobile.
- Fill rate (phones): cap
devicePixelRatioat 2 (or 1.5), fewer full-screen transparent layers, smaller particles.
Memory: gets slower over time
- Arrays that only grow, listeners added per restart, intervals never cleared, three.js objects not
dispose()d. Test: restart 10 times and compare heap snapshots.
3. Loading
- Total size: aim < 10–20 MB for web games; first playable < 3–5 MB. Shrink images and audio (bash-web-asset-shrinker): WebP/PNG-8, 128 kbps or lower audio, compressed glTF (Draco/meshopt), KTX2 textures.
- Load what the title and level 1 need first; stream the rest in the background.
- Show progress. Decode images (
img.decode()) and audio before the first frame that uses them, so there’s no hitch on first use. - Warm up shaders and effects once during loading (render a hidden explosion) to avoid first-use stalls.
4. Adaptive quality (phones heat up)
// if average frame time stays high for 2 s, step quality down: dpr 2 → 1.5 → 1, particles × 0.5, shadows off
if (ftAvg > 20 && performance.now() - lastChange > 2000 && quality > 0) { quality--; applyQuality(quality); lastChange = performance.now(); }
Offer a “Low graphics” setting too. Keep gameplay identical across quality levels.
Report
Say what you measured (before → after numbers), what you changed, and on which device or size. Example: “Phone frame time 31 ms → 12 ms: cached the tilemap, pooled bullets, capped pixel ratio at 2.”