Is not having local functions a micro optimisation?

后端 未结 5 718
执念已碎
执念已碎 2020-12-21 15:36

Would moving the inner function outside of this one so that its not created everytime the function is called be a micro-optimisation?

In this particular case the

5条回答
  •  一整个雨季
    2020-12-21 16:05

    It completely depends on how often the function is called. If it's a OnUpdate function that is called 10 times per second it is a decent optimalisation. If it's called three times per page, it is a micro optimalisation.

    Though handy, nested function definitions are never needed (they can be replaced by extra arguments for the function).

    Example with nested function:

    function somefunc() {
        var localvar = 5
    
        var otherfunc = function() {
             alert(localvar);
        }
    
        otherfunc();
    }
    

    Same thing, now with argument instead:

    function otherfunc(localvar) {
        alert(localvar);
    }
    
    function somefunc() {
        var localvar = 5
    
        otherfunc(localvar);
    }
    

提交回复
热议问题