Performance
Games that run inefficiently, such as those with heavy CPU usage or excessive memory allocation leading to frequent garbage collection pauses, may receive reduced visibility in user timelines/feeds.
Caching Actors
Caching frequently created actors is often necessary to prevent garbage collection pauses.
For example, the tetris game example uses a cache to store the tetrominoes instead of creating a new entity/Actor each time a new tetromino/piece is spawned:
const cache = new Map<string, Piece>();
const getPiece = (type: PieceType) => {
const existing = cache.get(type.name);
if (existing) {
existing.reset(); // reset to clean/initial state
return existing;
}
const piece = new Piece(type);
cache.set(type.name, piece);
return piece;
};
// ...
function spawn() {
const randomPieceType = random.pickOne(PIECE_TYPES);
const piece = getPiece(randomPieceType);
// ...
}