I work on a WebSocket server using NodeJS, and I really need to be able to search for the "class" of both the socket and the user ID.
So, I'm trying to figure out what will be more effective and why.
var usersBySocket = {};
var usersById = {}
server.on('connection', function(client) {
var networkClient = new NetworkClient(client);
usersBySocket[client] = networkClient;
usersById[networkClient.getId()] = networkClient;
});
OR
var users = {}
server.on('connection', function(client) {
users[client] = new NetworkClient(socket);
});
function getUserById(id) {
for(var uid in users) {
if(uid == id) return users[uid];
}
return undefined;
}
I am inclined to the first option, but I'm not sure how exactly JavaScript handles values, if each "associative array" is stored by value, and not by reference, this is a complete waste. I especially do not want an obsessive copy of memory.
source
share