Below is a snippet of code from the Socket.IO site for server code:
io.sockets.on('connection', function(socket) {
socket.emit('news', {hello:'world'});
socket.on('my other event', function(data) {
console.log(data);
});
});
What I can say by reading this code is that when connected, each socket independently determines its events. Although, since the same function is used to determine these events, they are all the same. That is, each socket has "my other event", which does the same, but each of them is a new instance of the function (wasteful).
My question is this: can the V8 optimize this obviously wasteful practice? Say using the same function, but in a different area. Since the scope is different in each function, I would suggest that any function referencing socketshould be defined separately, effectively cloning this event for each individual connection.
Is there some kind of optimization I'm missing here, or is this a very bad practice? This also applies to any other instance of this practice, and not just to the SIO; this is the most important case.
In addition, an apology if this was poorly conveyed, I can try to clarify if necessary.
user1948076
source
share