使用匿名函数会影响性能吗? [英] Does use of anonymous functions affect performance?

查看:318
本文介绍了使用匿名函数会影响性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,在Javascript中使用命名函数和匿名函数之间是否存在性能差异?

I've been wondering, is there a performance difference between using named functions and anonymous functions in Javascript?

for (var i = 0; i < 1000; ++i) {
    myObjects[i].onMyEvent = function() {
        // do something
    };
}

vs

function myEventHandler() {
    // do something
}

for (var i = 0; i < 1000; ++i) {
    myObjects[i].onMyEvent = myEventHandler;
}

第一个更整洁,因为它不会使代码混乱,很少 - 使用了函数,但是你多次重新声明该函数是否重要?

The first is tidier since it doesn't clutter up your code with rarely-used functions, but does it matter that you're re-declaring that function multiple times?

推荐答案

这里的性能问题是在循环的每次迭代中创建新函数对象的成本而不是您使用匿名函数的事实:

The performance problem here is the cost of creating a new function object at each iteration of the loop and not the fact that you use an anonymous function:

for (var i = 0; i < 1000; ++i) {    
    myObjects[i].onMyEvent = function() {
        // do something    
    };
}

您正在创建一千个不同的函数对象,即使它们具有相同的主体代码并且没有绑定到词法范围(结束)。另一方面,以下似乎更快,因为它只是在整个循环中为数组元素分配相同的函数引用:

You are creating a thousand distinct function objects even though they have the same body of code and no binding to the lexical scope (closure). The following seems faster, on the other hand, because it simply assigns the same function reference to the array elements throughout the loop:

function myEventHandler() {
    // do something
}

for (var i = 0; i < 1000; ++i) {
    myObjects[i].onMyEvent = myEventHandler;
}

如果要在进入循环之前创建匿名函数,则只需指定在循环内部向数组元素引用它时,您会发现与命名函数版本相比,没有任何性能或语义差异:

If you were to create the anonymous function before entering the loop, then only assign references to it to the array elements while inside the loop, you will find that there is no performance or semantic difference whatsoever when compared to the named function version:

var handler = function() {
    // do something    
};
for (var i = 0; i < 1000; ++i) {    
    myObjects[i].onMyEvent = handler;
}

简而言之,使用匿名优于命名函数没有可观察到的性能成本。

In short, there is no observable performance cost to using anonymous over named functions.

顺便说一下,从上面看,它们之间没有区别:

As an aside, it may appear from above that there is no difference between:

function myEventHandler() { /* ... */ }

和:

var myEventHandler = function() { /* ... */ }

前者是函数声明,而后者是匿名函数的变量赋值。尽管它们似乎具有相同的效果,但JavaScript确实对它们的处理方式略有不同。为了理解这种差异,我建议阅读 JavaScript函数声明歧义

The former is a function declaration whereas the latter is a variable assignment to an anonymous function. Although they may appear to have the same effect, JavaScript does treat them slightly differently. To understand the difference, I recommend reading, "JavaScript function declaration ambiguity".

任何方法的实际执行时间很大程度上取决于浏览器的编译器和运行时的实现。有关现代浏览器性能的完整比较,请访问 JS Perf站点

The actual execution time for any approach is largely going to be dictated by the browser's implementation of the compiler and runtime. For a complete comparison of modern browser performance, visit the JS Perf site

这篇关于使用匿名函数会影响性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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