Ani*_*ish 14 javascript performance function
差异b/w函数声明和函数表达式在var functionName = function(){} vs function functionName(){}中有详细描述.
在此提到函数声明在分析时评估,函数表达式在执行相
在bytes.com中,提到函数声明比函数表达式更快.
我为此创建了一个基本的测试用例:http://jsperf.com/function-declaration-vs-function-expression
功能声明:
function myfunc() {
alert("yo");
}
myfunc();
Run Code Online (Sandbox Code Playgroud)
功能表达:
var myfunc = function() {
alert("yo");
}
myfunc();
Run Code Online (Sandbox Code Playgroud)
测试表明函数表达比函数声明慢90%.
为什么速度如此差异?
编辑:
来自http://jsperf.com/function-declaration-vs-function-expression中的结果
In Chrome, IE9, Opera & Safari- > Function Declaration比Function Expression更快
In Firefox, IE7, IE8- > Function Expression比Function Declaration更快
在IE9中,函数声明更快,而在IE 7和8中函数表达更快.是因为IE9中的JavaScript引擎发生了变化,还是故意这一举动?