Game Memory Allocation
Fine in update() / render()
Section titled “Fine in update() / render()”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.
Usually avoid every frame
Section titled “Usually avoid every frame”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.
Prefer persistent objects
Section titled “Prefer persistent objects”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;Use pooling for many short-lived objects
Section titled “Use pooling for many short-lived objects”For particles, bullets, or enemies created and removed often, reuse inactive instances instead of repeatedly calling new.
Practical rule
Section titled “Practical rule”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.