# How Myaku works

## Frames

Myaku wraps `requestAnimationFrame`. The first callback with a new timestamp starts a frame. A frame's **interval** runs from its start to the next frame's start, which is the gap the player sees. Its **CPU time** is how long its frame callbacks ran.

An interval is never shorter than the frame's CPU time. After a callback runs 30 ms, Chrome can deliver the next frame with a timestamp right back on the 16.7 ms grid, so timestamps alone would call the frame on time. Long Animation Frames only reports frames over 50 ms, so it doesn't see these either. They're the hitches players feel most often.

## GPU work

Myaku wraps `getContext` on canvases and offscreen canvases. On each WebGL context it counts `draw*` calls, texture and buffer uploads with their byte sizes, and shader compiles and program links. On 2D contexts it counts drawing calls. Contexts created before Myaku starts aren't counted, which is why it should load first.

## Attribution

Chromium reports [Long Animation Frames](https://developer.chrome.com/docs/web-platform/long-animation-frames): for any frame over 50 ms, which scripts ran, from which file and function, and how long style and layout took. Myaku keeps these entries and matches them to frames by time.

Because Myaku's wrapper calls your frame callbacks, the browser attributes them to myaku. So when a callback is first registered, Myaku records where it came from (one stack capture per distinct callback, cached), and points the verdict there instead.

## Engine spans

Engines are found where they announce themselves, with no change to the game: PixiJS's and three.js's devtools hooks, Excalibur's devtools global, and Phaser's and KAPLAY's globals. PixiJS can call its hook before its ticker exists, so Myaku finishes hooking up once `init()` has run. Browsers round `performance.now()`, so a cheap span often measures 0 ms. It still appears in reports, so an engine that wasn't hooked up looks different from a fast one. See [spans](./spans).

## Garbage collection

In Chromium, `performance.memory` gives the heap size. A drop of a megabyte or more between frames, while the game's code was idle, is a strong sign of a collection.

## Cost

Frames live in fixed-size typed arrays, so recording a frame doesn't allocate, and Myaku doesn't cause the GC it's looking for. Its own time per frame is measured and reported as `overheadMs`. Verdicts are only computed when you ask for them.

The first 10 frames (scripts compiling, assets decoding) are warm-up and aren't counted as play.

## Measurement scope {#what-myaku-can-t-see}

- **GPU time.** A GPU-bound frame shows as `unknown`, with a hint when draw calls are high.
- **Inside an engine's loop,** without [spans](./spans).
- **Script locations outside Chromium.** Other browsers still get frame times, counts and spans.
- **Workers.** Only the main thread is profiled.