Does the placement of a function have an effect on the performance of closures within scope? If so, where is the optimal place to put these functions? If not, is the implied association by closure enough reason to place a function in another place logically?
For instance, if foo does not rely on the value of localState, does the fact that localState is accessible from foo have implications as to foo's execution time, memory use, etc.?
(function(){
var localState;
function foo(){
// code
}
function bar(){
// code
return localState;
}
})();
In other words, would this be a better choice, and if so why?
(function(){
function foo(){
// code
}
var localState;
function bar(){
// code
return localState;
}
})();
Darius Bacon has suggested below that the two samples above are identical since localState can be accessed anywhere from within the block. However, the example below where foo is defined outside the block may be a different case. What do you think?
function foo(){
// code
}
(function(){
var localState;
function bar(){
// code
foo();
return localState;
}
})();