In JavaScript, what are specific reasons why creating functions within a loop can be computationally wasteful?
On page 39 of JavaScript the Good Parts, Douglas Crockford states, “Avoid creating functions within a loop. It can be computationally wasteful”. I can’t seem to figure out why creating functions within a loop would be anymore wasteful than outside.
Because you’re creating several
Functionobjects instead of reusing just one.Example of creating an identical function…
Example of reusing a named function…
The two examples have the same outcome, but the second doesn’t require the overhead of making two functions during the loop. Instead it creates just the one handler.