Posts Tagged ‘Benchmark’

AS3 Short Variable/Function Names vs Long Ones

August 30th, 2006 8 comments

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 …

	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.