Tiny strict object pool for hot acquire/release paths: sprites, bullets, particles, DOM nodes, and worker slots.
Status: 0.5.9 - stable 1.0-track surface. The root entry is the public API.
pnpm add aipooljsimport { createPool } from "aipooljs";const bullets = createPool({
size: 256,
create: () => ({ x: 0, y: 0, active: false }),
reset: (b) => {
b.x = 0;
b.y = 0;
b.active = false;
},
});
const bullet = bullets.acquire();
bullet.active = true;
bullets.release(bullet);
await bullets.borrow(async (slot) => {
slot.active = true;
});
bullets.dispose();createPool({ size, create, reset, onOverflow? })builds a fixed-size pool.pool.acquire()returns a live slot or followsonOverflow.pool.release(obj)resets and returns a live slot.pool.drain()releases all currently live slots.pool.borrow(fn, { signal }?)acquires, runsfn, and releases infinally.pool.dispose()is idempotent permanent teardown.- Read-only state:
available,alive,disposed. - Errors:
PoolError,PoolDisposedError.
| Strategy | Behavior |
|---|---|
"throw" |
Default; throws PoolError when empty. |
"null" |
acquire() returns null; factory overload narrows the pool type. |
"grow" |
Allocates more objects and doubles capacity; can cause same-frame GC spikes. |
| function | Escape hatch. The handler returns a slot and is responsible for avoiding aliasing/recursion. |
- Function overflow handlers can return an already-live object. That aliases the previous holder and can make later
release()calls throw. - If
reset()throws, the slot is removed from alive before it returns to available, so capacity shrinks permanently for that object. borrow()only takes the async branch whenfnreturns a nativePromise. Non-instanceof Promisethenables are treated as sync: the slot is released immediately and the thenable is returned as-is. Prefer realPromises.- Aborting
borrow()releases the slot but does not stop inner work. If the pool is disposed during async borrow, the result is reported through the disposed path. - Do not
deletefields inreset()on hot objects; assign default values to preserve hidden classes.
- Short index:
llms.txt - Full generated context:
llms-full.txt - Stability contract:
STABILITY.md - Current review backlog:
REVIEW.md - Release history:
CHANGELOG.md
MIT