package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.utils.*; public class TestConcat extends Sprite { private var iteration_count:int=0 private var concat_total:int=0 private var slice_total:int=0 private var clone_total:int=0 private var tf:TextField = new TextField() private var test_array:Array = []; public function TestConcat():void { tf.x = tf.y = 100; tf.width = 600; addChild(tf) //Set up array to copy for(var i:int = 0; i < 1000000; i++) test_array.push(i); //Mouse click to rerun test stage.addEventListener(MouseEvent.CLICK, go); //First run go() } private function go(e:Event = null):void { iteration_count=concat_total=slice_total=clone_total=0 addEventListener(Event.ENTER_FRAME, iterate) } //Loop through tests private function iterate(e:Event=null):void { concat_total +=testConcat() slice_total += testSlice() clone_total += testByteArrayClone() iteration_count++ tf.text = "Av. Concat time=" + (concat_total / iteration_count) + "ms Av. Slice time=" + (slice_total / iteration_count) + "ms Av. BA Clone time=" + (clone_total / iteration_count) + "ms"; if(iteration_count<99) removeEventListener(Event.ENTER_FRAME,iterate) } //test array slice private function testSlice():int { var time_slice_start:Number = getTimer(); var slice_copy:Array = test_array.slice(0); return getTimer()-time_slice_start } //test array concat private function testConcat():int { var time_concat_start:Number = getTimer(); var concat_copy:Array = test_array.concat(); return getTimer()-time_concat_start } //test BA Clone method private function testByteArrayClone():int { var time_concat_start:Number = getTimer(); var concat_copy:Array = clone(test_array); return getTimer()-time_concat_start } //Clone method for Deep Objects(via Bruno) private function clone(source:Object):* { var myBA:ByteArray = new ByteArray(); myBA.writeObject(source); myBA.position = 0; return(myBA.readObject()); } } }