Skip to content

Game Memory Allocation

const speed = 200;
let nextX = player.x + speed * dt;
const { ball, player1, player2 } = this.activeProps;

Local variables and primitive values are cheap. Object destructuring creates local bindings, not copies of the referenced objects.

const ball = new Ball();
const position = { x, y };
const trail = [];
const copy = { ...props };
const rest = { ...other };

These create new objects or arrays each frame. A few may be harmless, but frequent allocations can increase garbage-collection pauses and cause frame-time spikes.

Create long-lived game objects during setup, in a constructor, or in enter():

constructor() {
super();
this.particlePool = [];
}

Then mutate and reuse them during update():

particle.x += particle.velocityX * dt;

For particles, bullets, or enemies created and removed often, reuse inactive instances instead of repeatedly calling new.

Prioritize readable code first. Profile before optimizing. Avoid obvious per-frame object, array, spread/rest, and closure creation in hot paths; ordinary local variables and property reads are not a concern.