Get frame timing and simulation right. requestAnimationFrame with a fixed simulation step and interpolated rendering, the same game speed on 30, 60, 120 and 144 Hz screens, dt caps and spiral-of-death protection, pausing on hidden tabs, frame-rate caps and render-on-demand for battery, separating update and render, avoiding layout thrash and long tasks in the loop, and smooth cameras. Includes ...
Download skill (.zip) Files on GitHubGame render loop optimization
Drop-in: assets/loop.js (tested: 60 updates/s on 30 Hz and 120 Hz screens, 5 s spikes capped, pause and fps cap work).
import { createLoop, lerp, snapshot } from "./core/loop.js";
const loop = createLoop({
step: 1 / 60, // simulation rate (fixed): same physics on every screen
update(dt) { for (const e of entities) { snapshot(e); e.update(dt); } },
render(alpha) { // alpha 0..1: blend between last and current state for smooth motion
for (const e of entities) draw(e, lerp(e.prevX, e.x, alpha), lerp(e.prevY, e.y, alpha));
},
onAutoPause() { scenes.push("pause"); }, // tab hidden → paused; resume only by the player
});
loop.start();
Rules
| Rule | Why |
|---|---|
| Fixed step for game logic, variable rate for rendering | Jump heights and collisions identical at every frame rate |
Interpolate rendering (alpha) |
Without it, 144 Hz screens show the 60 Hz simulation as judder |
| Cap the frame dt (0.25 s) and the number of steps per frame (8) | A tab switch or breakpoint doesn’t fast-forward or freeze the game (“spiral of death”) |
requestAnimationFrame only, never setInterval |
rAF matches the display and pauses in background tabs |
Pause on visibilitychange |
No hidden-tab drift; saves battery |
| Read input in update, not in event handlers | Consistent timing; edges cleared per step |
| No DOM reads or writes per entity per frame | Layout thrash. Update HUD text only when the value changes |
| No big synchronous work mid-game (level generation, JSON parse, image decode) | Long tasks = visible hitches. Do it during loading or a transition |
Choosing the step
- 60 Hz for action games (default). 30 Hz simulation is fine for strategy, idle and puzzle games (cheaper, still render at 60).
- Very fast physics (racing, pinball): 120 Hz simulation steps, or substeps for the fast parts only.
- Turn-based or menu-only screens:
onDemand: true+loop.requestRender()when something changes (game-battery-optimization).
Smooth motion checklist
- Camera follows the interpolated player position. Smoothing with
damp(frame-rate independent), notx += (t - x) * 0.1. - Round only at draw time (pixel art), never in the simulation.
- Animations: advance by
dtin update, choose the frame in render. - Check the frame-time graph (game-performance-profiling): flat at 16.7 ms is the goal; regular spikes mean GC or periodic work.
Engines
Phaser and three.js have their own loops: Phaser fps: { target, forceSetTimeOut: false } and Arcade physics fixedStep: true;
three.js renderer.setAnimationLoop with your own fixed-step accumulator (threejs-game-development).