Developer Optimization: if vs. function, what’s better?

A really great question about optimization was asked in one of the adobe community forums, so I thought, let's try to answer it. Even though, there is no great answer to this really really good question. so the question is: Optimization, if or multiple

A really great question about optimization was asked in one of the adobe community forums, so I thought, let’s try to answer it. Even though, there is no great answer to this really really good question. so the question is: Optimization, if or multiple function calls?

The developer here basically is asking us What is better of? Is it to use a for loop with a dynamic function calling based on what the value in the variable is? or is it to separate it into based on the else conditions.

The real answer is that the top one is probably more efficient, but it’s such a good question that I wanted to explain first the question itself.

```for (var x = 10; value % x == 0 && x < 1,000,001; x *= 10){
this[ "functionName_" + x.toString() ](x);
}
```
```for(var x = 10; value % x == 0 && x < 1,000,001; x*=10){
if(x == 10){
//do 10 effect
} else if (x == 100){
//do 100 effect
} else if (x == 1000){
//do 1k effect
} else if...{
//all values up to 1m
}
}
```

What is the logic in here?

I’m going to go into my Adobe flash here, and I already pasted inside his code but I pasted it in a little bit differently.
Note: From the efficiency perspective for the developer itself is to make sure that he defines his variables outside of his for loop in this case, because I could go to really high values, I wanted have instead as a ‘uint’.

As developer describes; each time this code is called the value in the variable value grows by one and in the for loop itself. What is actually doing here the variable i starts with the value of 10 and each round it duplicates itself by 10. Before doing so the for loop tests two questions:
1. does the value variable divide by i without leftovers.
2. Is the value smaller than 1,000,001

Note: I changed his variable from ‘x’ to ‘i’ because I pasted it right into our display, into the root and x is a reserved variable because it’s part of the display properties.

Let’s go back into his question and try to figure out what is the answer. In this case the maxim amount of times the for loop would execute would be 6 times. If the value is less than a million the amount of runs would be shorter.

Example:

Now, if I put here a value… let’s say I put here 500,000 then it won’t run throughout the whole million
but it’s only going to run until 100,000. Where it will not find anything further than that and it will stop soon after.

If I put into value the value of 101, it’s going to automatically, as soon as I run it terminate that for loop. Because of this definition the value would always have a remainder of one and as such automatically stop the loop.

From the efficiency perspective, this is really efficient coding, really beautiful and complex to look at.
It took me a few minutes to realize what’s actually going on in there. Back into the question itself: We see there are 6 actions going on here that’s the maximum amount of actions that could be.

While the second option on the get go looks more complex. In reality for small number of iterations the second option would run faster – that is because functions are more taxing then if statements. But as the amount of loops grows the second code becomes very inefficient. Let’s see why… First is here it’s a lot more complex, the reason that it’s more complex is because if the value is 10 right one if statement will be tested vs one function = in other words a function is more taxing so the function is less efficient.

If the script runs let’s say 10 times then the for loop needs to run through 10 if statements vs. 1 function in that case a function is already starting to be more efficient. In other words, the more times the code runes the less efficient the second option is in general.

I’m automatically adding a lot more taxing, a lot more things in general. now if the question in general… are functions more efficient than if conditions? not necessarily! Functions take something from the processor a lot more then a condition does.

so it is a good question, in general I would lean to this because it’s just cleaner, but I would say that
really big question about performance is not if you should do this way or that way. The second option is probably not a nice way to do it at all. For a different rational, there is a duplication of both a for loop and conditions. As such, the code will be bloated and complicated to read, take a lot of lines of text and over time if large enough become less efficient. As such, my suggestion would be even through for smaller values the second option would run faster. I wouldn’t suggest to use it for the rational provided above and in the video.

In the second case, you could probably skip the for loop all together because you already listing out all the different terms. If you’re listing out all the different terms you just don’t need a for loop. Because you’re running through all of them anyways.

If you do that in this way and not using a for loop that could be most efficient way but would take a lot of typing. Once you decided to use a for loop the idea of running through each case becomes irrelevant as it defeats the point of a for loop. The for loop is just not needed anymore. As such, it’s an obvious never.

With that said, if your code is running slow and its a critical area that demands more optimization you might want to test it out but not in a normal operation. It might be a very efficient way without the for loop but would demand a lot more development and for most scenarios it would not be worth the advantage.

I would say, also looking to the question of what are the do-action do effects doing?
Is there any way to unify those do effects into one method?
if there is, that probably would be the most efficient way and I would definitely go for the first option (functions).

I hope this answered your question, See you until next time I see an interesting question  