In the following jsperf: http://jsperf.com/defined-function-vs-in-loop-function/3
You will notice that this code:
for (var i = 0; i < loops; i++) {
function func(a, b) {
return a + b;
};
func(i, i);
}
Performs on par with this code:
function declaredFn(a, b) {
return a + b;
};
for (i = 0; i < loops; i++) {
declaredFni, i);
}
But this code:
for (i = 0; i < loops; i++) {
var func = function(a, b) {
return a + b;
};
func(i, i);
}
is significantly slower then this code:
var expfunc = function(a, b) {
return a + b;
};
for (i = 0; i < loops; i++) {
expfunc(i, i);
}
Why? What is happening internally?