V8内部-匿名函数的处理 [英] V8 Internals - Handling of Anonymous Functions

查看:54
本文介绍了V8内部-匿名函数的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关全文,请查看我的其他问题.

基本上,我问过以下代码在套接字处理程序中使用命名函数是否更有效:

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
    });
});

总体答案是肯定的(有关更多详细信息,请参见上面的链接),但是以下评论由 ThiefMaster 发布.:

The overall answer was yes (see the above link for more details), but the following comment was posted by ThiefMaster:

我对V8内部并不熟悉,但是它可能足够聪明,可以一次编译该函数,然后每次使用它,只要附加一个不同的作用域即可.

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.

所以这是我的问题.V8是否足够聪明,可以一次编译匿名函数并在匿名函数通常导致创建多个函数实例的情况下在不同范围内重用它们?例如,上面我希望 connection 事件的处理程序创建一次,但 action1 action2 的处理程序将被创建一次.为每个连接创建的断开连接.在另一个问题中,这是通过命名函数解决的,但是我对在V8中是否有必要或是否会进行一些优化更感兴趣.

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.

推荐答案

是.我提出了一个非常相似的问题(在我的案例中与创建V8邮件列表中的其他函数).我得到的答复是,即使每次都有一个单独的函数 object (按规范要求),该函数的 code 仍是"...正常重用...".

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
    });
});

它使用了一个命名函数表达式,该表达式完全有效并且可以由V8正确处理.(遗憾的是,这是未由IE8和更早版本正确处理的,这会创建两个在完全不同的时间具有完全不同的功能.但是在使用V8时,您不必为此担心.)

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 completely different functions at totally different times. But as you're using V8, you don't have to worry about that.)

这篇关于V8内部-匿名函数的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆