AS3 Short Variable/Function Names vs Long Ones
In ActionScript it was common knowledge that shorter Variable and Function names yielded better performance in situations were it depends but does that still hold true for ActionScript 3.0? I’ve ran a couple of quick tests to find a clear answer about this. I usually tend keep my Variable/Function names to 1-3 characters when performance really mattered but it might actually not be necessary. Consider the following test …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package { import com.hexagonstar.util.debug.Debug; import flash.utils.getTimer; import flash.display.Sprite; public class PerformanceTest extends Sprite { private var aVeryVeryLongVariableNameaVeryVeryLongVariableName:Number; private var num:Number; public function PerformanceTest() { var startTime:Number = getTimer(); for (var i:int = 0; i < 4000000; i++) { aVeryVeryLongVariableNameaVeryVeryLongVariableName = (i * Math.random()) * 100; } var timeTaken:String = getTimer() - startTime + " ms"; Debug.trace("timeTaken (long): " + timeTaken); } } } |
I’ve ran this code ten times with the 50 characters long variable name and then ten times with a 3 characters long one, both in their own SWF’s. The results ranged from 757ms to 820ms, more or less the same on both sides. The sum of the long variable version was 7924ms and the one of the three characters long one was 7903ms, not much of a difference.
The next test was similar but this time with a 50 characters long Method name and the other again with a 3 characters long one, the Methods returned a random Number for 4000000 times during this test. The results looked similar but the total sum of each test gave a 12044ms for the long name test and 15570ms for the short named one. A noticable difference of over 3 seconds. This might have been coincidence but I’ve ran the tests a couple more times and it turned out every time that the result were very close together. This is quite interesting because I was safely believing that short names would still have an advantage. It might be time to rethink this.

I stopped worrying about variable names back in Flash MX. It’s probably the least bang you can get for the buck in terms of optimization. The “buck” being unreadable code, and the bang being the milliseconds you’d save. Yeah, you saved 3 seconds there, but I can’t remember the last 4 million iteration loop I ran.
I’v just done the same test on AS2 some days ago here : http://www.foxaweb.com/blog/?p=41 #04 Length of var names.
I perform the bench on 10 000 itérations a frame, so I think the result is not much different in AS3.
Like Peters, I don’t take care about this kind of optimization since it’s realy deserve the lisiblity.
Well when AS2.0 went live there were some statements flying around that name length still matters. Good to know that it wasn’t really that much of problem. With AS3.0 it seems we can finally forget about this issue completely.
I’m not saying that there is no difference, even in AS3, I actually don’t know. What I’m mainly saying is that any difference is going to be relatively small, and at the expense of obfuscating your code. I’ve actually gravitated towards longer, more descriptive variable names as time has gone by.
Back in the days of Flash 5, such hacks were more important, as the Flash player was so slow, anything you could do do squeeze a few more milliseconds out of it was worth it. Also, in those days people were just doing more web sites, games, animations, etc. Now that the player is SOOO much faster, and people are doing full fledged applications with hundreds of classes, good development practices are FAR more important than optimization tricks.
Of course, if you are doing some kind of crazy, cpu killing animation, you might find something like this helps a bit, but I can bet that in any swf that has any significant amount of code, I could find some other optimization that would save even more time than shortening your var names.
Keith you are right! … But you should know I speak with the mind of a game programmer whose roots are in the 80ties 8bit times!
People always say “I will never make a 4000 iterations loop, so 100, 200 ms does not worth it”.
Just a word of warning: when programming games or graphic intense applications, for a 60 FPS you got 16.67 ms to perform ALL your drawing routines… So, yes, each ms counts for!
For programmers like me from de 80’s when we had 3.58Mhz (yes, mhz, not ghz) and only about 32Kb of RAM (yes, kb, not mb, not gb), every optimization counts
Otherwise will be a “Vista”: doesn’t matter performance. Let our users buy faster and expensives machines. =(
(p.s.: preety weird this post form uh? Labels beneath the fields????)