/dm-game:game-perffrom dm-game
Optimizes per-frame JS/TS game code by identifying allocation anti-patterns (spread, .map/.filter) and providing GC-friendly pooled or pre-allocated alternatives.
Ships in dm-game. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Sources: These are standard JavaScript engine behaviours (generational GC, hidden-class stability) rather than game-specific findings — see V8's public documentation on allocation and garbage collection. Spatial hashing is textbook broad-phase collision detection. No figures here are benchmarks; measure your own hot path with a profiler before optimising it, per the Numbers Policy in game-design.
This skill provides patterns for writing allocation-free, GC-friendly code in game loops and hot paths. Apply these patterns proactively when working on any code that executes per-frame.
Problem: Spread creates a new array every call.
// BAD: Creates new array every frame
const context = {
enemies: [...this.enemies],
projectiles: [...this.projectiles],
};
Fix: Pass readonly references.
// GOOD: Zero allocation
const context = {
enemies: this.enemies as readonly EnemyState[],
projectiles: this.projectiles as readonly ProjectileState[],
};
Problem: filter() always creates a new array.
// BAD: New array every call
const activeEnemies = enemies.filter(e => e.active);
Fix: In-place filtering with swap-and-truncate.
// GOOD: Mutate in place
function filterInPlace<T>(array: T[], predicate: (item: T) => boolean): void {
let writeIndex = 0;
for (let i = 0; i < array.length; i++) {
if (predicate(array[i])) {
array[writeIndex++] = array[i];
}
}
array.length = writeIndex;
}
Problem: map() creates a new array.
// BAD: New array every frame
const positions = enemies.map(e => e.worldPos);
steering.separation(ctx, positions, radius);
Fix: Scratch array or inline iteration.
// GOOD: Reuse scratch array
const positionsScratch: Vec2[] = [];
function getPositions(enemies: readonly EnemyState[]): readonly Vec2[] {
positionsScratch.length = 0;
for (const e of enemies) {
positionsScratch.push(e.worldPos);
}
return positionsScratch;
}
Problem: Double allocation.
// BAD: Two new arrays
const activePositions = enemies
.filter(e => e.active)
.map(e => e.worldPos);
Fix: Single-pass with scratch array.
// GOOD: Single pass, zero allocation
const scratch: Vec2[] = [];
function getActivePositions(enemies: readonly EnemyState[]): readonly Vec2[] {
scratch.length = 0;
for (const e of enemies) {
if (e.active) scratch.push(e.worldPos);
}
return scratch;
}
Problem: Helper functions that return new arrays per call.
// BAD: New array per entity per frame
function getWrappedPositions(pos: Vec2): Vec2[] {
const positions = [pos];
// ... add wrapped positions
return positions;
}
Fix: Module-level scratch with readonly return.
// GOOD: Reusable scratch buffer
const scratchPositions: Vec2[] = [];
function getWrappedPositions(pos: Vec2): readonly Vec2[] {
scratchPositions.length = 0;
scratchPositions.push(pos);
// ... add wrapped positions
return scratchPositions;
}
The readonly return type signals to callers: "consume immediately, do not store."
Problem: Checking every entity against every other entity.
// BAD: O(n²) - checks all enemies for each enemy
for (const enemy of enemies) {
const nearby = enemies.filter(e =>
e !== enemy && distance(e.pos, enemy.pos) < radius
);
}
Fix: Spatial hash grid for O(n) build + O(1) queries.
// GOOD: Build grid once, query many times
const grid = new Map<string, Entity[]>();
const CELL_SIZE = 100;
function buildGrid(entities: readonly Entity[]): void {
grid.clear();
for (const e of entities) {
const key = `${Math.floor(e.pos.x / CELL_SIZE)},${Math.floor(e.pos.y / CELL_SIZE)}`;
if (!grid.has(key)) grid.set(key, []);
grid.get(key)!.push(e);
}
}
function queryNearby(pos: Vec2, radius: number): readonly Entity[] {
scratch.length = 0;
const cx = Math.floor(pos.x / CELL_SIZE);
const cy = Math.floor(pos.y / CELL_SIZE);
// Check 3x3 cells
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
const cell = grid.get(`${cx + dx},${cy + dy}`);
if (cell) {
for (const e of cell) {
if (distance(e.pos, pos) < radius) scratch.push(e);
}
}
}
}
return scratch;
}
Problem: Creating temporary objects inside loops.
// BAD: New object per iteration
for (const enemy of enemies) {
const ctx = { position: enemy.pos, velocity: enemy.vel };
updateAI(ctx);
}
Fix: Reuse a single context object.
// GOOD: Reuse context object
const ctx = { position: { x: 0, y: 0 }, velocity: { x: 0, y: 0 } };
for (const enemy of enemies) {
ctx.position.x = enemy.pos.x;
ctx.position.y = enemy.pos.y;
ctx.velocity.x = enemy.vel.x;
ctx.velocity.y = enemy.vel.y;
updateAI(ctx);
}
// Per-frame setup phase
buildSpatialGrid(entities);
buildEnemyGrid(enemies);
// Per-entity query phase (many times)
for (const entity of entities) {
const nearby = queryNearby(entity.pos, RADIUS);
// process nearby...
}
When a function returns a readonly array, it communicates:
For entities created/destroyed frequently (particles, projectiles):
class Pool<T> {
private available: T[] = [];
acquire(factory: () => T): T {
return this.available.pop() ?? factory();
}
release(item: T): void {
this.available.push(item);
}
}
Removing from a collection while iterating it is the most common source of skipped-entity bugs, and the naive fix — copying the collection every frame — is an allocation in the hot path. Mark, then sweep once.
// BAD - mutates during iteration; silently skips the following element
for (const e of world.asteroids) {
if (e.dead) world.asteroids.delete(e);
}
// GOOD - mark during systems, sweep once after all of them have run
world.destroy(e); // sets a flag, pushes to a reused pending array
// ...all systems run...
world.flush(); // single pass; clears pending without reallocating
When the world is larger than the screen, per-entity render work for off-screen
entities is pure waste. Set visible = false rather than skipping the update —
most renderers then discard the object before it reaches the GPU.
function inViewport(x: number, y: number, r: number, vp: Rectangle): boolean {
return x + r > vp.x && x - r < vp.x + vp.width &&
y + r > vp.y && y - r < vp.y + vp.height;
}
Performance isn't just an engineering concern — it constrains design decisions. Feed these constraints back into design early:
| Performance Constraint | Design Implication |
|---|---|
| Entity count cap (e.g., 500 at 60fps) | Limits enemy density, particle counts, projectile counts — affects encounter design |
| Spatial hash cell size | Determines minimum meaningful distance between entities — affects spacing design |
| Collision check budget | Limits simultaneous interacting entities — affects group combat design |
| Draw call budget | Limits visual complexity per frame — affects VFX and juice design |
| Memory budget | Limits world size and asset variety — affects content scope |
Design rule: Establish performance budgets BEFORE designing encounters, particle effects, or entity populations. A design that requires 2000 entities at 60fps on a budget that supports 500 is not a performance problem — it's a design problem. See encounter-design and systems-design for design-level responses to performance constraints.
Before committing changes to per-frame code:
[...array]) on arrays that don't changefilter() / map() / reduce() creating new arrays{}) or array literals ([]) inside loopsreadonly for scratch buffersThree tight links. Everything else routes through the map, so adding a skill
touches one file rather than twenty: references/routing-map.md (in
game-design).
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub rbergman/dark-matter-marketplace --plugin dm-game40 skills in this plugin
/accessibility-designUse when designing any player-facing feature, evaluating a game for accessibility, responding to accessibility feedback, designing difficulty or assist options, adding subtitle/caption systems, implementing input remapping, or when a player reports they can't play. Covers the four accessibility pillars (visual, auditory, motor, cognitive), implementation tiers, colorblind design, subtitle standards, input accessibility, and testing methodology. Accessibility is a design discipline, not a post-launch checklist./async-and-socialSocial and competitive design that does not require players to be online together: ghost/replay data, leaderboards, asynchronous PvP, offline vulnerability, and anti-snowball mechanics. Use when adding leaderboards or high-score tables, designing ghost races or replay challenges, letting players attack or compete against absent opponents, deciding what social signal a single-player game should carry, or when matches and runs feel decided in the first minutes. Anti-snowball applies to single-player roguelikes and simulations too, not just PvP. For real-time multiplayer architecture, matchmaking, guilds, and moderation, this skill deliberately does not cover them — those are large specialities and belong in dedicated references.Other plugins' skills for the same job