I usually code like this to begin with:
[as]for(var i:int = 0; i < whatever; i++) { var n:Number = someCalculation goes here; somethingElse = n * whatever; }[/as] Then when I decide to optimize, and I’m looking to squeeze that one last millisecond out of my code, I start doing stuff like this: [as]var i:int; var n:Number; for(i = 0; i < whatever; i++) { n = someCalculation goes here; somethingElse = n * whatever; }[/as] The thinking being that you declare the local vars up front, rather than in the loop. This wasn’t one of those things I ever really thought about in much depth, just one of those fixed ideas you get in your head and you operate on until you actually step back and think about it for two seconds. Today, I thought about it for two seconds and realized that for the i at least, this was pretty silly. It still only gets declared once, even if you declare it in the loop like the first example. Duh. Then I started thinking about the n. Sure, it’s declared in the loop, so theoretically, that declaration line gets run every time through the loop. But since AS3 does not have block level local vars, it’s still scoped to the function, and probably only actually gets created once. Doing some simple speed tests confirmed that there really is no difference between the two. Now I need to go back and refactor a bunch of “highly optimized” code so I don’t look like an ass. 🙂 But, I’d really feel even better about it if one of you people who read AS3 byte code over lunch could step in and validate my findings.