Is it better to exit a function to cut activation objects than recursively or call nested functions?

In JavaScript and other languages, I heard about activation objects created when a method / function is called. To optimize and maintain good performance, this sounds like a developer must limit how many functions are called.

Now, if there is no way, and you have to call several methods, is it better to call one method after another, for example:

myFunc1(); myFunc2(); myFunc3(); // or... var myFuncs = [myFunc1, myFunc2, myFunc3]; for(var a=0, aLen=myFuncs.length; a<aLen; a++) { myFuncs[a](); } 

OR to nest them like this:

 function myFunc1() { // Do something... myFunc2(); } function myFunc2() { // Do Something else... myFunc3(); } function myFunc3() { //Do one last thing. } //Start the execution of all 3 methods: myFunc1(); 

I suppose it makes sense to go with the 1st technique, as it goes back to the previous area and releases the last activation object ... but if someone can confirm this, I really would like to know!

thanks

+4
source share
3 answers

For information about activation objects, see http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/#more-546

However, this does not apply to the level of optimization, since the problem you mentioned is an example of preliminary optimization of EXTREME, and your time is not worth this type of investment. And in fact, the above example doesn’t save much when you look only at activation objects.

However, for proper use, I try to encapsulate as much as possible. If a function does not have to go in a global scope and can live within another function, then it must be declared.

for example, for a better overview.

 var f2 = function() { } var f1 = function() { f2() } // is not as nice as: var f1 = function() { var f2 = function() f2() } // or even better.. var f1 = function() { function() { }() ; execute } 
+1
source

To optimize and maintain good performance, this sounds like a developer must limit how many functions are called.

Yes and no. Functions (or, more generally, subroutines) should be called, and it does not make sense to not do this. If you can make your code drier by introducing another function, do it.

The only place where they are not used is reasonable, these are high-performance loops that work thousands of times but do not work much, and function calls will add noticeable overhead. Do not try to optimize prematurely !

In addition, there are some languages ​​that handle recursion poorly and where you need to translate recursive function calls into loops, except for stackoverflow exceptions. However, this is also a rare case.

is it better to call one method after another or insert them?

It depends on how the two methods do different things. In # 1 there are only 3 independent functions called one after another. In contrast, # 2 defines functions that always call each other - you cannot get myFunc2 without myFunc3 . Is this intended?

If so, there is nothing wrong with this nest. Two additional stack layers will not hurt your performance.

+3
source

Separation of responsibility:

 private function myFunc1(): void { } private function myFunc2(): void { } private function myFunc3(): void { } private function doAllFunc(): void { myFunc1(); myFunc2(); myFunc3(); } 
0
source

All Articles