For the full story, check out my other question.
Basically, I had asked if it were more efficient to use named functions in the socket handlers for the following code:
var app = require('express').createServer()
var io = require('socket.io').listen(app);
app.listen(8080);
// Some unrelated stuff
io.sockets.on('connection', function (socket) {
socket.on('action1', function (data) {
// logic for action1
});
socket.on('action2', function (data) {
// logic for action2
});
socket.on('disconnect', function(){
// logic for disconnect
});
});
The overall answer was yes (see the above link for more details), but the following comment was posted by ThiefMaster:
I'm not familiar with V8 internals but it might be smart enough to compile the function once and re-use it everytime, just with a different scope attached.
So now that's my question. Is V8 smart enough to compile anonymous functions once and reuse them with different scopes in situations where anonymous functions ordinarily lead to several function instances being created? For example, above I would expect the handler for the connection
event to be created once but the handlers for action1
, action2
, and disconnect
to be created for each connection. In the other question this was solved with named functions but I am more interested if this is necessary in V8 or if it will do some optimizations.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…