函数声明比函数表达式更快? [英] function declaration faster than function expression?

查看:77
本文介绍了函数声明比函数表达式更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同的b / w函数声明&函数表达式在 var functionName = function(){} vs function functionName(){} 中有精彩的描述


在这里提到函数声明是在解析时间和&函数表达式在执行阶段被评估

bytes.com 提到函数声明比函数表达式快。



我创建了一个基本的测试用例为此: http://jsperf.com/function-declaration-vs-function-expression



函数声明

  function myfunc(){
alert(yo);
}
myfunc();

函数表达式

  var myfunc = function(){
alert(yo);
}
myfunc();

测试显示函数表达式比函数声明慢90%

为什么这样的速度有所不同?

编辑

http://jsperf.com/function-declaration-vs功能表达式



Chrome,IE9,Opera& Safari - >函数声明比函数表达式更快

Firefox,IE7,IE8中 - >函数表达式比函数声明更快

IE9中函数声明更快,而在IE 7& 8函数表达式更快。 是因为IE9中JavaScript引擎的变化,还是此举有意为之? 解决方案

Firefox也有非标准的 Function Statements ,这使得可以在函数声明之间进行有条件的选择(根据规范,你不能)。只需使用JuriykangaxZaytsev 的示例:

  if(true){
function foo(){return 1; }
} else {
function foo(){return 2; }
}
foo(); // 1
//注意其他客户端在这里将`foo`作为函数声明,
//用第二个函数覆盖第一个`foo`,并产生2,而不是1结果

所以那些是在执行时编译的,而不是在parse阶段:


函数语句在变量实例化过程中不被声明。它们是在运行时声明的,就像函数表达式一样。

其他浏览器可能会预编译函数声明,使它们在运行时间,但Firefox必须在运行时解释函数声明,导致基于Gecko的浏览器中的函数声明和函数表达式之间的速度没有多大区别。


The difference b/w function declaration & function expression is beautifully described in var functionName = function() {} vs function functionName() {}
In this it's mentioned that function declaration is evaluated during parse-time, & function expression is evaluated in the execution-phase

In bytes.com it's mentioned that function declaration is faster than function expression.

I created a basic test case for this: http://jsperf.com/function-declaration-vs-function-expression

Function Declaration:

function myfunc() {
 alert("yo");
}
myfunc();

Function Expression:

var myfunc = function() {
 alert("yo");
}
myfunc();

The test showed that function expression is 90% slower than function declaration.

Why such a difference in speed?

Edit:
From the results in http://jsperf.com/function-declaration-vs-function-expression

In Chrome, IE9, Opera & Safari -> Function Declaration is faster than Function Expression

In Firefox, IE7, IE8 -> Function Expression is faster than Function Declaration

In IE9 Function declaration is faster, whereas in IE 7 & 8 function expression is faster. Is it because of change in JavaScript engine in IE9, or was this move intentional?

解决方案

Firefox also has non-standard Function Statements, which makes it possible to conditionally choose between function declarations (per spec, you can't). Just using an example of Juriy "kangax" Zaytsev:

if (true) {
    function foo(){ return 1; }
} else {
    function foo(){ return 2; }
}
foo(); // 1
// Note that other clients interpet `foo` as function declaration here, 
// overwriting first `foo` with the second one, and producing "2", not "1" as a result

So those are compiled at execution time, instead of in the parse-phase:

Function statements are NOT declared during variable instantiation. They are declared at run time, just like function expressions.

Other browsers probably will pre-compile function declarations, making them perform faster at run time, but Firefox has to interpret function declarations at run time, causing not much difference between the speed of function declarations and function expressions in Gecko-based browsers.

这篇关于函数声明比函数表达式更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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