# Optimisation tips

This guide gives memory, CPU and rendering advice so a game stays smooth on low-end laptops, phones with throttled CPUs and 4 GB Chromebooks; a game that runs well there runs everywhere. Examples that use `SDK` assume `const SDK = window.ZokoGames.SDK;` and `await SDK.init();` as in the [SDK introduction](https://docs.zokogames.com/sdk/introduction#loading). Unfamiliar terms are in the [glossary](https://docs.zokogames.com/getting-started/glossary).

## 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](https://docs.zokogames.com/requirements/gameplay).

## 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:

```js
let playing = false;                      // set true when a round starts, false when it ends
document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    loop.stop();
    if (playing) SDK.game.gameplayStop().catch(() => {});
  } else {
    loop.start();
    if (playing) SDK.game.gameplayStart().catch(() => {});
  }
});
```

Both SDK calls are asynchronous and throw `sdkDisabled` when the SDK is off (for example a standalone run), hence the `.catch`. Pausing on hidden also keeps your measured playtime honest, because the portal counts the time between `gameplayStart` and `gameplayStop` (see the [metrics reference](https://docs.zokogames.com/guides/analytics-metrics)).

## 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 `AudioContext` on first gesture (see [technical requirements](https://docs.zokogames.com/requirements/technical#audio-on-ios)).

## 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.

## Next steps

- [Getting to the first frame fast](https://docs.zokogames.com/guides/first-frame)
- [Mobile, touch and orientation](https://docs.zokogames.com/guides/mobile)
