Given the following two:
Scenario 1
function inner() {
// a bunch of code that does stuff
}
function outer() {
inner();
}
for(var i = 0; i < 10000; i++) {
outer();
}
Scenario 2
function outer() {
function inner() {
// a bunch of code that does stuff
}
inner();
}
for(var i = 0; i < 10000; i++) {
outer();
}
Behavior is identical in both cases, no doubt. But what's the difference under the hood? How much extra work, if any, is the interpreter doing in scenario 2? Is the memory affected. Or, say, if the body of inner() gets longer, would that increase the effect on performance?
Please don't bother asking "why would you want to do that", because my question is not about a practical issue. Just trying to get a deeper understanding of how JS function are parsed and represented. Thanks!
Closure. You can use it for different things: - as a function factory - as a private method which can be called only from the outer functionouteris as much a closure asinneris), this is not an answer to what the OP asked.