Performance tips
A Fabi Play game runs on TVs: weak GPUs, slow CPUs, small memory, sometimes no usable WebGL. Your laptop says nothing about them. These habits keep a game smooth everywhere.
Make once, change every frame
Section titled “Make once, change every frame”- Create nodes once (at the start, or when something appears) and change their fields every frame. Don’t rebuild the scene each frame.
- Destroy what leaves the screen (
node.destroy()), and reuse what comes back often (hide it, move it, show it). - No new objects, arrays or closures per frame in hot loops: on a TV the garbage collector’s pauses are dropped frames, right when a child reaches for a star.
Bake, don’t draw
Section titled “Bake, don’t draw”- Shapes and texts are baked once and then moved as images: cheap. Changing a text’s string or a shape’s
specbakes it again: fine for a score, not for something that changes every frame. - Scores, counters and timers: draw them with a bitmap font. It bakes nothing when the
text changes, so a countdown in tenths of a second costs no more than a still label. Or show a bar (
roundRect, or any frame nine-sliced, resized freely). - Draw your own art once with
engine.bake, never per frame. Paths, gradients and shadows are what Canvas 2D on a TV’s CPU cannot do 60 times a second. - Reuse shape descriptions: shapes with the same description share one baked image, and a few dozen different ones fit the atlas easily. Hundreds of unique shapes or texts fill it, and a full atlas is wiped and baked again.
Count with the tier
Section titled “Count with the tier”- Scale particle pools and burst sizes, and anything numerous, with
engine.tier.scale(n): × 1 on high, × 0.5 on medium, × 0.2 on low. - Use additive glows (
blend: 'add') only whenengine.tier.effectsis true. - Shadows and glows on shapes and texts are dropped below high by the engine. Make sure the flat look still reads: outlines are cheap on every tier.
Clip freely, mask sparingly
Section titled “Clip freely, mask sparingly”- A clip that is not rotated is nearly free: use it for lists and windows.
- Each mask, and each rotated clip, costs about two full-canvas composites a frame: the node and its mask are drawn into layers the size of the canvas. A few are fine; one per object is not. On a TV’s GPU, and on Canvas 2D, filling the whole canvas twice more is what drops frames.
- Masked art that doesn’t move inside its mask is better baked once (see Clips and masks).
- Faint avatars (
engine.avatars({ alpha })below 1) use the same layers: one composite per avatar.
Move by time
Section titled “Move by time”- Move everything by
dt(x += speed * dt). TVs run at 50 Hz, 60 Hz, or slower when busy, anddtis the only thing that keeps speeds right on all of them. - Use
onFramefor game logic (it stops on pause) andonRenderonly for what must move during a pause.
Keep the scene small
Section titled “Keep the scene small”- A whole Fabi Play scene (background, avatars, a few hundred stars and particles, text) is a handful of draw calls.
engine.stats.drawCallsandengine.stats.quadsshow what yours costs. - Use groups to hide whole layers (
visible = false) instead of hiding many nodes one by one. - Keep moving things on the canvas. HTML (
engine.hud) is for a few static overlays: dozens of moving HTML elements each become a compositor layer, which TVs handle badly, especially over the video.
Load before ready
Section titled “Load before ready”- Load everything with
engine.loadbeforeplay.ready(): Fabi Play’s loader covers the wait. Say ready within 10 seconds. - Keep files small: every game ships its own copy of everything, over a TV’s Wi-Fi. Use the lite build with script tags.
Test the weak case
Section titled “Test the weak case”?renderer=canvas2d&tier=lowon your game’s address shows the weakest case on your laptop (see Forcing a renderer or a tier), or the simulator’s «Motore» panel: its status line shows the renderer, tier and frame rate the engine reports.- Chrome’s DevTools CPU throttling (4× or 6×) gets closer to a TV’s CPU.
- Then try a real TV or a cheap tablet: nothing replaces the device.
Filters
Section titled “Filters”Each filter costs a layer and a pass per step, over the filtered node’s area: a few filters on
big nodes are fine on a TV, one per sprite is not. On tiers that cannot afford a filter the engine skips it and
counts it in engine.stats.filtersSkipped (see Filters).
Creating nodes, and node pools
Section titled “Creating nodes, and node pools”Create and destroy nodes freely. Making a sprite or a shape costs well under a microsecond (a shape’s bake is cached by its description: the second star with the same spec costs no bake), and a steady frame of moving, tweening sprites, shapes, texts and particles makes almost no garbage (about 2 KB a frame on a scene of 240 quads, short-lived numbers only), so garbage collection does not pause a TV.
Reach for a node pool only when a game spawns many nodes a second (dozens: bullets, coins,
floating labels) and engine.stats.cpuMs or pauses on the weakest TV show it. Texts are the exception: each new text
bakes its string, so a “+10” label made 20 times a second is worth pooling sooner.
Nodes are handles
Section titled “Nodes are handles”A node’s transform and look (x, y, rotation, scaleX, scaleY, alpha, visible, tint) and its world
matrix live in typed arrays inside the engine, one slot per node; the node object you hold is a small handle on its
slot. You use it exactly as before (star.x += 120 * dt). One difference: node.world is a view on those arrays,
and the arrays are replaced when they grow, so read node.world when you need it rather than keeping it in a
variable across frames (a kept one stops following the node).
What does not move costs (almost) nothing. Setting a node’s position, rotation, scale, alpha or visible marks
it; while drawing, the engine composes the world matrix of a marked node and of everything under it, and keeps the
others’ as they were. A still background, a HUD or a menu of hundreds of nodes costs no matrix math frame after
frame, and a node that moves without turning or scaling costs no cosine. Setting a field to the value it already
has still marks the node: in a loop over many still nodes, skip the write.
After destroy(), the handle is stale. Its slot goes back to the engine and, later, to a new node. A stale
handle never touches that new node:
- reading its fields right after
destroy()still gives its last values (burst(coin.x, coin.y)aftercoin.destroy()works); - once its slot is another node’s, reads give a fresh node’s values (
x0,alpha1,visiblefalse) and writes do nothing; node.destroyedistruefrom the moment it is destroyed;- tweens and timelines on it stop by themselves.
It never throws. In development builds the first write to a destroyed node says so in the console, once (see below). Drop your references to destroyed nodes all the same: a stale handle is a bug that no longer breaks other nodes, not a feature.
Destroy what you are done with. A group (with everything in it) or a node taken out with remove() that you drop
without destroy() gives its slots back when the browser collects it; a single node made and never added anywhere
keeps its slot (about 160 bytes) until the page closes.
Development warnings
Section titled “Development warnings”Development builds check for the most common pooling mistake: using a node after destroy(), or after release() to
its pool (the pool gives it to the next get(), so a kept reference would move someone else’s node). The first time
such a node’s x, y, scale, rotation, alpha, visible, tint, frame, text, filters, clip or mask
is set, it is added to a group, or a tween is started on it, the console says what and where, once per node:
[engine] Sprite 'coin' used after release() to its node pool: setting x. It is not on the stage, and the pool gives it to the next get(): drop your reference to it.| Build | Checks |
|---|---|
npm, bundler in development (development export condition: Vite, webpack) |
on |
| npm, production build | off (not in the code) |
script tag *.umd.js (readable) |
on |
script tag *.umd.min.js, fabi-play-engine[.lite].js |
off (not in the code) |
bundling the engine’s source (src/, inside the workspace) |
off, unless your bundler defines __DEV__ as true (or the page sets globalThis.__DEV__ = true before the engine runs) |
The checks cost nothing on live nodes, even in development: only destroyed or released nodes are watched.