Reuse game objects (bullets, particles, enemies, pickups, popups, sounds) instead of creating and throwing them away, so garbage collection doesn't cause stutters. A tested Pool with prewarming, a max size, reset hooks, O(1) swap-remove during iteration and stats, plus a ring buffer for overwrite-oldest effects. Covers what to pool, resetting state fully, pooling in Phaser and three.js, and mea...
Download skill (.zip) Files on GitHubGame object pooling
Creating thousands of short-lived objects per second makes the garbage collector pause the game (stutters every few
seconds, worse on phones). Pool anything that spawns often and dies soon. Drop-in: assets/pool.js (tested).
import { Pool, Ring } from "./core/pool.js";
const bullets = new Pool(() => ({ x: 0, y: 0, vx: 0, vy: 0, life: 0, hit: false }), {
prewarm: 200, max: 500, // allocate at load, cap at 500 live
reset: (b) => { b.hit = false; }, // clear EVERY field that matters, or old state leaks
});
bullets.spawn((b) => { b.x = px; b.y = py; b.vx = 600; b.vy = 0; b.life = 1.2; }); // returns null at max
bullets.update((b) => { b.x += b.vx * dt; return (b.life -= dt) > 0 && !b.hit; }); // false → back to the pool
bullets.forEach((b) => ctx.fillRect(b.x - 2, b.y - 2, 4, 4));
bullets.releaseAll(); // on restart / level end
const sparks = new Ring(300, () => ({ x: 0, y: 0, life: 0 })); // particles: overwrite the oldest, never allocate
update walks backwards and swap-removes, so removing during iteration never skips an item (the classic forward-splice bug).
What to pool
| Pool | Don’t bother |
|---|---|
| Bullets, particles, sparks, damage numbers, coins, enemies in waves, explosions, trails, decals, audio voices | The player, bosses, level geometry, UI screens, things created once per level |
Rules
- Reset completely. A reused enemy with last round’s
hp,targetorstateis a classic pooling bug. Reset inresetor fully in thespawninitializer. - Don’t keep references to pooled objects after they’re released (a homing missile targeting a released enemy now
targets a new enemy). Check an
aliveflag or a generation counter:if (target.gen !== savedGen) target = null. - Prewarm to the typical peak (check
stats().peakafter a hard level) so nothing allocates mid-fight. - A
maxturns an explosion of spawns into a graceful limit instead of a slowdown;stats().rejectedshows if it’s too low. - Also avoid other per-frame garbage: reuse vectors (
tmp.set()), no.map/.filterin hot loops, no closures per entity per frame.
Engines
- Phaser:
this.physics.add.group({ classType: Bullet, maxSize: 200, runChildUpdate: true });group.get(x, y)reuses inactive members;setActive(false).setVisible(false)to release. - three.js: never create and dispose meshes per shot. Use
InstancedMeshwith a fixed count and hide unused instances (scale 0), or a pool of meshes that togglevisible. Share geometry and material.
Prove it
Profile before and after (game-performance-profiling): heap peak − start should drop, and p99 frame time with it. “Heap churn 9 MB/s → 0.3 MB/s; p99 31 → 17 ms.”