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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…