Which is the fastest way to store data: Vector or ByteArray? Given that you can upload both types to Stage3D in Flash Player 11, this question has never been more relevant. So which should you use to maximize your app’s speed? Read on for the performance testing.

To test the two classes out, I made an app that simply fills a Vector and a ByteArray using various approaches:

  • Vector index ([]) operator
  • ByteArray index ([]) operator
  • ByteArray.writeByte
  • ByteArray.writeInt
  • ByteArray.writeDouble

I used these to fill out these structures:

  • Vector.<int>
  • Vector.<Number>
  • ByteArray of bytes
  • ByteArray of ints
  • ByteArray of Numbers/doubles

Here is the source code for the test:

package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
 
	public class VectorVsByteArray extends Sprite
	{
		private var logger:TextField = new TextField();
		private function row(...cols): void { logger.appendText(cols.join(",")+"\n"); }
 
		public function VectorVsByteArray()
		{
			logger.autoSize = TextFieldAutoSize.LEFT;
			addChild(logger);
 
			var SIZE:int = 1000000;
			var beforeTime:int;
			var afterTime:int;
			var i:int;
			var vi:Vector.<int>;
			var vf:Vector.<Number>;
			var ba:ByteArray;
 
			vi = new Vector.<int>(SIZE);
			vf = new Vector.<Number>(SIZE);
			ba = new ByteArray();
 
			row("Test", "Time");
 
			beforeTime = getTimer();
			for (i = 0; i < SIZE; ++i)
			{
				vi[i] = 33;
			}
			afterTime = getTimer();
			row("Vector int", (afterTime-beforeTime));
 
			ba.length = SIZE;
			ba.position = 0;
 
			beforeTime = getTimer();
			for (i = 0; i < SIZE; ++i)
			{
				ba[i] = 33;
			}
			afterTime = getTimer();
			row("ByteArray byte (index)", (afterTime-beforeTime));
 
			ba.length = 0;
			ba.position = 0;
 
			beforeTime = getTimer();
			for (i = 0; i < SIZE; ++i)
			{
				ba.writeByte(33);
			}
			afterTime = getTimer();
			row("ByteArray byte (writeByte)", (afterTime-beforeTime));
 
			ba.length = 0;
			ba.position = 0;
 
			beforeTime = getTimer();
			for (i = 0; i < SIZE; ++i)
			{
				ba.writeInt(33);
			}
			afterTime = getTimer();
			row("ByteArray int (writeInt)", (afterTime-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < SIZE; ++i)
			{
				vf[i] = 33.3;
			}
			afterTime = getTimer();
			row("Vector Number", (afterTime-beforeTime));
 
			ba.length = 0;
			ba.position = 0;
 
			beforeTime = getTimer();
			for (i = 0; i < SIZE; ++i)
			{
				ba.writeDouble(33.3);
			}
			afterTime = getTimer();
			row("ByteArray Number (writeDouble)", (afterTime-beforeTime));
		}
	}
}

I ran this test on the following environment:

  • Flex SDK (MXMLC) 4.5.1.21328, compiling in release mode (no debugging or verbose stack traces)
  • Release version of Flash Player 11.1.102.63
  • 2.4 Ghz Intel Core i5
  • Mac OS X 10.7.3

And got these results:

Test Time
Vector int 16
ByteArray byte (index) 34
ByteArray byte (writeByte) 69
ByteArray int (writeInt) 63
Vector Number 18
ByteArray Number (writeDouble) 90

Performance Chart

Given this performance data, we can draw some conclusions:

  • Vector is 2-6x faster at writing out data than ByteArray
  • Using the index operator with ByteArray is much faster than ByteArray.writeByte
  • Writing Number/double values is slower than bytes or ints, especially via ByteArray

One caveat to the above results is that the Alchemy opcodes can be used to much more efficiently use ByteArrays. Unfortunately, these opcodes are not available in pure AS3 application so you’ll need to use a third-party tool like Apparat or language like HaXe to get access to them. Beware though as these opcodes will no longer be supported in the upcoming release of Flash Player 11.2.

So for now the best pure AS3 method of storing lots of data very quickly is Vector. By a lot.

Spot a bug? Have a suggestion? Post a comment!