Overview
Set utilities provide functions for manipulating JavaScript’s Set
data structure. These utilities are particularly useful for managing unique collections of game objects, handling event listeners, and maintaining state.
Set Operations
Filtering Sets
Create a new set containing only elements that match a condition:
import { filterSet } from ' dill-pixel ' ;
const numbers = new Set ([ 1 , 2 , 3 , 4 , 5 ]);
const evenNumbers = filterSet (numbers , ( num ) => num % 2 === 0 );
Accessing Elements
Get the first or last element from a set:
import { firstFromSet, lastFromSet } from ' dill-pixel ' ;
const items = new Set ([ ' a ' , ' b ' , ' c ' ]);
const first = firstFromSet (items); // 'a'
const last = lastFromSet (items); // 'c'
Common Use Cases
Active Entity Management
import { filterSet } from ' dill-pixel ' ;
private entities = new Set < GameEntity >();
// Filter active entities
return filterSet ( this . entities , ( entity ) => entity . isActive );
getEntitiesByType ( type : string ) {
return filterSet ( this . entities , ( entity ) => entity . type === type);
Event Listener Management
import { filterSet } from ' dill-pixel ' ;
private listeners = new Set <{ event : string ; callback : Function }>();
// Get listeners for specific event
getEventListeners ( eventName : string ) {
return filterSet ( this . listeners , ( listener ) => listener . event === eventName);
// Remove inactive listeners
this . listeners = filterSet ( this . listeners , ( listener ) => ! listener . isExpired );
Queue Management
import { firstFromSet, lastFromSet } from ' dill-pixel ' ;
private tasks = new Set < Task >();
const nextTask = firstFromSet ( this . tasks );
this . tasks . delete (nextTask);
return lastFromSet ( this . tasks );
Type Safety
The set utilities are fully typed and support generic types:
const players = new Set < Player > ();
const onlinePlayers = filterSet (players , ( player ) => player . isOnline );
const highScorers = filterSet (players , ( player ) => player . score > 1000 );
// Type-safe element access
const firstPlayer : Player | undefined = firstFromSet (players);
const lastPlayer : Player | undefined = lastFromSet (players);
Best Practices
Use appropriate operations for your needs:
// Good: Using filterSet for complex filtering
const activePlayers = filterSet (players , ( p ) => p . isActive && p . health > 0 );
// Less ideal: Manual filtering
const active = new Set ([ ... players] . filter ( ( p ) => p . isActive ));
Handle undefined cases:
const first = firstFromSet (set);
Consider performance with large sets:
// Cache filtered results when appropriate
private cachedActive = new Set < Entity >();
private needsUpdate = true ;
this . cachedActive = filterSet ( this . entities , ( e ) => e . isActive );
this . needsUpdate = false ;
return this . cachedActive ;
Efficient Usage
// Less efficient: Filtering on every frame
const activeEnemies = filterSet (enemies , ( e ) => e . isActive );
processEnemies (activeEnemies);
// More efficient: Cache and update as needed
private activeEnemies = new Set < Enemy >();
markActive ( enemy : Enemy ) {
this . activeEnemies . add (enemy);
this . activeEnemies . delete (enemy);
return this . activeEnemies ;
Tips and Tricks
Combine with other utilities:
import { filterSet, intBetween } from ' dill-pixel ' ;
function getRandomSubset < T > ( set : Set < T >, count : number ) {
return filterSet (set , () => intBetween ( 0 , 2 ) === 1 );
Chain operations efficiently:
function getEligiblePlayers ( players : Set < Player > ) {
filterSet (players , ( p ) => p . isOnline ) ,
Use with type guards:
function isValidPlayer ( player : unknown ) : player is Player {
return typeof player === ' object ' && player !== null && ' id ' in player && ' isOnline ' in player;
const validPlayers = filterSet (players , isValidPlayer);