Skip to content Skip to sidebar Skip to footer

In Javascript, What Are Specific Reasons Why Creating Functions Within A Loop Can Be Computationally Wasteful?

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, 'A

Solution 1:

Creating a function can use a lot of resources. As functions are in fact objects, the code has to actually create a new function object each time, it can't just create it once and then just reuse it.

Creating a function inside a loop usually means that you will be creating a lot of functions instead of just creating a single function outside the loop.

So, it's just a matter of not doing something resource intensive inside a loop if you can instead do it once outside the loop. When it comes to functions, they can often be created outside the loop without changing the logic of the code.

Post a Comment for "In Javascript, What Are Specific Reasons Why Creating Functions Within A Loop Can Be Computationally Wasteful?"