I wrote a chat program in Node.js. The server manages a list of rooms in a class called RoomManager
with an object this.rooms
. Whenever a command is sent to the server from a client, it goes into a router. Most of the commands deal with rooms (sending messages to rooms, joining rooms, creating rooms..). The command message must always have a room ID in it. The server must always try and find the room first:
roomManagerGetRoom(roomName){
return this.rooms[roomName] || null;
}
Since the rooms
object will eventually get large, I figured the clients should keep their own references to what rooms they are in. That way, the server could first try and find the room from the client's own list which would be much smaller, 1-4 rooms on average.
However, this creates a cyclical loop
RoomManager
requies Room
requires ClientManager
requires Client
requires RoomManager
requies Room
requires ClientManager
requires Client
...
So 1.5 questions:
- How much speed would I be gaining by first looking through
Client
's own list? If it's significant, how do I avoid the cyclical issue?