1

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:

  1. 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?
jozenbasin
  • 2,142
  • 2
  • 14
  • 22
  • 2
    `how do I avoid the cyclical issue` by redesigning your "objects" - hard to say without any real idea of why all those objects require what they require – Jaromanda X Jan 21 '17 at 05:37
  • `Client` is the culprit. Without a doubt. But I only gave it it's own `RoomManager` so that i could avoid looking through the `rooms` object often and am worried that it will affect performance if the object grows. If that's not an issue than I avoid this problem. – jozenbasin Jan 21 '17 at 05:40
  • I found this post which might help: http://stackoverflow.com/a/24775197/2813263 – Domino Jan 21 '17 at 06:21

0 Answers0