最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - V8 Internals - Handling of Anonymous Functions - Stack Overflow

programmeradmin1浏览0评论

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 ment was posted by ThiefMaster:

I'm not familiar with V8 internals but it might be smart enough to pile 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 pile 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.

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 ment was posted by ThiefMaster:

I'm not familiar with V8 internals but it might be smart enough to pile 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 pile 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.

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Apr 15, 2012 at 7:24 knpwrsknpwrs 16.6k12 gold badges66 silver badges104 bronze badges 1
  • 1 Note that this has nothing to do with the function being named or anonymous; it has to do with whether you create a new function on each connection. (See my answer for more on that.) – T.J. Crowder Commented Apr 15, 2012 at 7:47
Add a ment  | 

1 Answer 1

Reset to default 9

Yes. I asked a very similar question (related in my case to creating functions from within a constructor function) on the V8 mailing list. I got the reply that the function's code is "...normally reused...", even though there's a separate function object each time (as required by the spec).


Note, though, that your question has nothing to do with whether the function is named or anonymous. The function in your example could have a name:

io.sockets.on('connection', function handleConnection(socket) {
    socket.on('action1', function (data) {
        // logic for action1
    });

    socket.on('action2', function (data) {
        // logic for action2
    });

    socket.on('disconnect', function(){
        // logic for disconnect
    });
});

That uses a named function expression, which is perfectly valid and handled correctly by V8. (Sadly, it's not handled correctly by IE8 and earlier, which create two pletely different functions at totally different times. But as you're using V8, you don't have to worry about that.)

发布评论

评论列表(0)

  1. 暂无评论