Optimisation tips
Optimisation tips
Browser games run on hardware you do not control: low-end laptops, phones with throttled CPUs, 4 GB Chromebooks. A game that runs smoothly there runs everywhere.
Frame budget#
At 60 Hz you have about 16 ms per frame; at 144 Hz about 7 ms. Profile with the browser's Performance panel. Fix the longest tasks first, and always use a time delta, never a fixed step per frame. See gameplay requirements.
Rendering#
- Batch draw calls: atlases, instancing, a single material per batch.
- Cap
devicePixelRatio. On phones and low-memory devices render at 1, and scale the canvas with CSS. High-DPI canvases quadruple fill rate and memory. - Do not redraw static layers every frame; cache them in an offscreen canvas.
- Pause rendering when the page is hidden:
document.addEventListener("visibilitychange", () => {
if (document.hidden) { loop.stop(); SDK.game.gameplayStop(); }
else { loop.start(); SDK.game.gameplayStart(); }
});Memory#
- Free what you do not need: unload level textures, close audio buffers, null large arrays.
- Avoid allocating in hot loops. Reuse objects and typed arrays so the garbage collector does not stutter.
- Keep decoded image memory in mind: a 2048x2048 texture is 16 MB uncompressed no matter how small the file is.
- Unity and similar engines: reduce the heap size setting to what the game needs; browsers can refuse large allocations.
Audio#
- Limit simultaneous voices.
- Reuse audio sources instead of creating one per sound effect.
- Unlock the
AudioContexton first gesture (see technical requirements).
Input latency#
- Read input in the same frame you draw.
- Avoid heavy work in event handlers.
Test on slow hardware#
Use the browser's CPU throttling (4x or 6x slowdown) and a throttled network. If you cannot find a low-end device, throttling is a good approximation. Test in Chrome, Safari and Firefox; fix any browser-specific bugs.
Ads and banners#
Ad requests and banner placement do not affect frame time, but pausing the game cleanly does. Pause logic, timers and physics on adStarted and verify nothing keeps running underneath the ad.