Zero-allocation render and update loops
performancegarbage-collectionflutterflamecanvasjavascriptgame-loop
The most common cause of frame drops (jank) in a hand-written 2D game is not drawing too much; it is
creating short-lived objects inside render() or update(). Every frame at 60 FPS that allocates a few
hundred Paint, TextPainter, Vector2 or {x, y} objects builds garbage-collector pressure that shows up
as periodic stutter, worst on the web. In the Spacebar project the same bug was "fixed" in sessions 3, 5
and 7 and kept coming back in every new component, so it is worth treating as a standing rule, not a
one-time cleanup.
The rule
- Never construct drawing objects (
Paint,TextPainter,MaskFilter, gradients, paths) inside a per-frame method. Pre-allocate them as fields (late final,static final, or in the constructor /onLoad()), and mutate only what changes (.color, alpha). - Text: build a
TextPainteronce, cache it, and rebuild only when the text or style actually changes. For pulsing text, quantize the rebuild (e.g. only when alpha changes by more than 5 steps) or build a small lazy cache over the real pulse range. - Constant blur: a
MaskFilter.blur()with a constant sigma is astatic constfield, never per frame. - Vectors and temporaries: reuse scratch objects or plain numeric locals. A per-frame
Random(42)(seen in a wreck component) is both an allocation and a logic bug. - In vanilla JS: pools for particles, numeric keys instead of string keys in maps, shared scratch arrays, no closures created per frame, scratch buffers sized from the viewport once.
- Editor and tooling code that never runs during gameplay may allocate freely; keep it out of the hot path.
Review checklist before committing a component
- Read every
render()/update()(or rAF callback) fornew, constructors, object literals, array literals,.map()/.filter(), string concatenation, and closures. - Check debug overlays too: in Spacebar a debug overlay was always visible in production and allocated paragraphs every frame. Gate debug UI behind a flag (F3) and cache its text.
- Count draw calls across all systems against a budget (see Flutter / Flame canvas rendering performance facts).
Measuring, not guessing
- In Node, heap-delta "leak" tests need a warm-up (about 3,000 iterations) and a check of growth versus N. Growth that falls as N grows is JIT warm-up, not a leak; two false leaks were chased before this was understood.
- In the browser, watch the FPS counter in the busiest scene (near many neon signs, many NPCs) rather than in an empty area.
Gotchas
shadowBlurin Canvas2D andMaskFilter.blurin Flutter are the most expensive single properties; ban them from systems with many instances and fake glow instead (Neon glow without blur and 2D ground-coverage tricks).- Fixing allocations in one pass is not enough; new components reintroduce them. Make it part of QA.
See also
- Flutter / Flame canvas rendering performance facts
- Fixed-timestep game loop for browser games
- Neon glow without blur and 2D ground-coverage tricks
Linked from
- Arcade game design toolkit (loops, enemies, scoring, structure)
- Fixed-timestep game loop for browser games
- Flutter / Flame canvas rendering performance facts
- Neon glow without blur and 2D ground-coverage tricks
- Rendering a tile map without a chunk cache
- Visual effects in Flame (particles, effects, decorators, shaders)