Skip to content

Commit

Permalink
go back to using a for-loop for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceau committed Nov 10, 2023
1 parent 911267a commit 86cd796
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/database/repositories/player_repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ export class PlayerRepository {
}

public static async findAllPlayersByGame(db: DB, ...gameId: number[]): Promise<Map<number, PlayerRecord[]>> {
const query = db.selectFrom("player").where("game_id", "in", gameId).orderBy(["player.game_id", "player.index"]);
const gameIdToPlayersMap = new Map<number, PlayerRecord[]>();

const query = db.selectFrom("player").where("game_id", "in", gameId).orderBy(["player.game_id", "player.index"]);
const playerRecords = await query.selectAll().execute();
const totalRecords = playerRecords.length;

const gameIdToPlayersMap = new Map<number, PlayerRecord[]>();
playerRecords.forEach((player) => {
// We care about performance so use a for-loop for speed
for (let i = 0; i < totalRecords; i++) {
const player = playerRecords[i];
const gameId = player.game_id;
const players = gameIdToPlayersMap.get(gameId) ?? [];
players.push(player);
gameIdToPlayersMap.set(gameId, players);
});
}

return gameIdToPlayersMap;
}
Expand Down

0 comments on commit 86cd796

Please sign in to comment.