Skip to content

Class: SparseSet<Component> ​

Component storage indexed by entity id. O(1) add / remove / get; dense iteration order is insertion order with swap-remove fills.

m_sparsePool[entityId] → dense index, or TOMBSTONE, or undefined m_densePool[denseIndex] → entityId at that dense position m_componentPool[denseIndex] → the component value

Remove uses swap-remove: copies the last dense entry over the removed slot, updates the moved entity's sparse pointer, then pops. The previous slot is marked TOMBSTONE so a subsequent Has(entityId) returns false even if the sparse slot was 0 (the "zero-index" bug we hit early on).

Type Parameters ​

Type Parameter
Component

Constructors ​

Constructor ​

ts
new SparseSet<Component>(): SparseSet<Component>;

Returns ​

SparseSet<Component>

Methods ​

Add() ​

ts
Add(entityId, component): Component;

Parameters ​

ParameterType
entityIdnumber
componentComponent

Returns ​

Component


Clear() ​

ts
Clear(): void;

Returns ​

void


Count() ​

ts
Count(): number;

Returns ​

number


ForEach() ​

ts
ForEach(callbackFn): void;

Parameters ​

ParameterType
callbackFn(entityId, component) => void

Returns ​

void


Get() ​

ts
Get(entityId): Component;

Parameters ​

ParameterType
entityIdnumber

Returns ​

Component


GetComponents() ​

ts
GetComponents(): readonly Component[];

Returns ​

readonly Component[]


GetDenseEntities() ​

ts
GetDenseEntities(): readonly number[];

Returns ​

readonly number[]


Has() ​

ts
Has(entityId): boolean;

Parameters ​

ParameterType
entityIdnumber

Returns ​

boolean


Remove() ​

ts
Remove(entityId): void;

Parameters ​

ParameterType
entityIdnumber

Returns ​

void


TryGet() ​

ts
TryGet(entityId): Component | undefined;

Hot-path accessor — single sparse-pool read, no double Has check. Returns the component or undefined if the entity isn't in the set.

Parameters ​

ParameterType
entityIdnumber

Returns ​

Component | undefined

© 2026 PlayAdGentic