Back in 2004, I was comparing loop performance in AS2 – for, while, and do while. That expanded to testing the same against JavaScript. The upshot (!) was that do-while and while were the fastest, and pretty close to each other, and much faster than for, in both JS and AS2. More surprising to me was how much faster JS was than AS2 – for simple math based operations across large numbers of iterations, JS was about 3 times faster than AS2.

With the VM2 used by AS3, that’s all changed. A lot. For simple operations (array population, string concatenation), AS3 is several factors faster than JS.

First, my the result of my initial benchmarks from 2004. Each loop was run over 100,000 iterations, and each statement was just a simple Math.exp(iteration). I ran each benchmark 10 times and averages the results.

flash player 7:
for: 2094
while: 652
do-while: 695

javascript (IE6)
for: 612
while: 156
do-while : 156

In AS3, we know that the fastest of the three types of loops (for, while, do-while) is the for loop (I believe this was a conscious decision by the engineers at Adobe). I won’t bother providing benchmarks results – this is pretty well established.

To start, I’ll populate an Array of 500,000 members with a value equal to the iteration count.

var start = getTimer();
var iterations:int = 500000;
var arr:Array = new Array(iterations);
for(var i:int = 0; i < iterations; i++){
  arr[i] = i;
}
trace(getTimer()-start);

Runs an average of 61 ms. Less than a tenth of a second, pretty respectable.

Same thing in javascript:

var start = new Date();
var iterations = 500000;
var arr = new Array(iterations);
for(var i = 0; i < iterations; i++){
  arr[i] = i;
}
alert(new Date()-start);

Runs an average of about 1435 ms – almost one-and-a-half seconds. Over 23 times slower than the AS equivalent. But, I was running a for loop in JavaScript, and we know the while loop is faster. Let’s try that

var start = new Date();
var iterations = 500000;
var arr = new Array(iterations);
while(iterations){
  arr[iterations] = iterations--;
}
alert(new Date()-start);

1380 average ms. Better, but still more than 20 times slower than AS3.

How about string concatenation? We know that’s much harder work, so we’ll reduce iterations to 100,000.

var start = new Date();
var iterations = 100000;
var str = '';
for(var i = 0; i < iterations; i++){
  str += '.';
}
alert(new Date()-start);

After waiting over 4 seconds for the first alert to appear, I wondered if there might have been a hiccup, so anomalous event that caused a problem. Running it 5 more times showed that was not the case. Average of 4235 ms each.

And AS3?

var start = getTimer();
var iterations:int = 100000;
var str:String = '';
for(var i:int = 0; i < iterations; i++){
  str += '.';
}
trace(getTimer()-start);

33 ms. Yes, 33 milliseconds. Over 130 faster than the JS equivalent.

A quick disclaimer: while I haven’t tested it yet, it wouldn’t surprise me if new Date() was slower than getTimer() – and that should probably be considered when evaluating these results. That said, a single call to new Date() does not take 4 seconds.

So based on this (admittedly limited) empyrical data, not only is AS3 faster than JS, it’s lightyears ahead of JS. Another reason to ignore the Jobs fan-boys of the world who been predicting the demise of Flash year in and year out. Perhaps hardcore JS developers can include a hidden swf in their applications to do the heavy lifting.