Which for loop is faster?

for (var u=0; u < users.length; u ++) {
      //do stuff on users
    }

vs


var uLength = users.length;
    for (var u=0; u < uLength; u ++) {
   //do stuff on users
    }

I know that in PHP the second one is faster because the first one would keep checking the length each loop.

Anyone know any good guides to actionscript optimization? I might be getting a little too nitpicky… lol

true vs TRUE

i ++; vs ++i;

Second one.

Casting improves performance as well btw.

for (var u:int = 0; u < uLength; u++) {
}

[QUOTE=DesignerMichael;2349248]
Anyone know any good guides to actionscript optimization? I might be getting a little too nitpicky… lol

true vs TRUE

i ++; vs ++i;[/QUOTE]

Those little optimizations tend not to matter too much. The huge one is what sekasi mentioned. Type your variables! This will not only help you debug code but it will make your code MUCH faster. It’s a big performance difference.

Also avoid doing mathematical operations with uint for some reason it’s much slower than a regular int.

Good luck!

Anyone know any good guides to actionscript optimization?

http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf
http://www.adobe.com/devnet/flex/articles/as3_tuning/fm_as3perf.pdf

Especially since TRUE doesn’t work and ++i and i++ do different things.

[ot]Do you remember that horrible thread where someone spent quite a few posts arguing about whether or not the post- or pre-increment operator was faster as the third statement in a for loop? If not, I don’t recommend trying to find it, but I’ll give you permission to hunt me down (in a giant tractor, perhaps) if I ever become so obsessed with performance that I start arguing with someone over something silly or trivial like that.[/ot]

You probably are… If you really needed the performance increases gained by doing any sort of optimization that is language-specific, then you probably wouldn’t be willing to admit that you were becoming too nitpicky.

Haha no I never saw that but I bet it was hilarious. If you do become like that, you better watch your back :hitman:

Well yes you are being too nit-picky. I’d only suggest worrying that much about optimization if you were doing very low level programming on a function that will be used millions of times on a platform with limited processing power.

Your second one is probably all for naught because you didn’t cast! Cast cast cast! Not just for optimization but for compiler correctness. Proper casting is one of the most effective ways to prevent run-time errors.

Using uint is slower because the processor uses ints natively, so when the instruction needs to be sent to the processor it will be converted anyways. Only use uints for super huge numbers that will never be negative.

From my experience using for in and for each loops are slower again. With the for in being slower than a for each.