Bash.tv → GitHub → itch.io

Object Pool

Reuses bullets, enemies and effects instead of making new ones, which avoids the stutters caused by garbage collection on phones.

Files: object-pool.js (the component, no dependencies) and object-pool.test.mjs (3 tests). Copy object-pool.js into your game, for example to src/components/, and import it as an ES module.

Usage

import { createPool } from "./object-pool.js";
const bullets = createPool(() => ({ x: 0, y: 0, vx: 0, vy: 0 }), { initial: 100, max: 300, reset: (b) => { b.vx = b.vy = 0; } });
const b = bullets.acquire(); if (b) { b.x = gun.x; b.vx = 600; }
bullets.forEach((b) => { b.x += b.vx * dt; if (b.x > W) bullets.release(b); });   // safe to release while looping

API

Engines

Engine Notes
Vanilla, Phaser, PixiJS, Three.js yes. With Phaser use Group (it pools already) for sprites; this pool suits plain data and Pixi sprites or Three meshes
(in create() make the sprite or mesh; in reset() hide it) (in create() make the sprite or mesh; in reset() hide it).

Tests

Node tests: reuse of the same objects, reset, the max limit, double release, release while looping, stats.

Run them with node --test toolkit/components/object-pool/ (Node 20 or newer, nothing to install).