In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Comparison Array.concat() with the trick using Function.apply() and Array.push()

Arrays can be joined with Function.apply() and Array.push() methods.  This sample compares this trick with Array.concat() method.
Get Adobe Flash player
by Fumio 12 Jan 2011
/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wGXS
 */

package {
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	[SWF(width = "240",height = "180")]
	public class ArrayConcatenation extends Sprite {
		private const COUNT_TRIAL:uint = 10000;
		private const COUNT_SOURCE:uint = 300;
		private const COUNT_ADDITION:uint = 300;
		private var addition_array:Array = createArray(COUNT_ADDITION);
		private var started:uint;
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		private var my_fmt:TextFormat = new TextFormat();
		public function ArrayConcatenation() {
			// Creating a TextField for display
			createTextField();
			// Starting test
			apply_push();
			use_concat();
			apply_push();
			use_concat();
			apply_push();
			use_concat();
			apply_push();
			use_concat();
		}
		private function apply_push():void {
			var nLength:uint = COUNT_TRIAL;
			var _array:Array = getArrays(COUNT_TRIAL);
			started = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				var my_array:Array = _array[i];
				my_array.push.apply(my_array, addition_array);
			}
			xTrace(getTimer() - started);
		}
		private function use_concat():void {
			var nLength:uint = COUNT_TRIAL;
			var _array:Array = getArrays(COUNT_TRIAL);
			started = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				var my_array:Array = _array[i];
				my_array = my_array.concat(addition_array);
			}
			xTrace(getTimer() - started);
		}
		private function getArrays(n:uint):Array {
			var _array:Array = [];
			for (var i:uint = 0; i < n; i++) {
				_array[i] = createArray(COUNT_SOURCE);
			}
			return _array;
		}
		private function createArray(n:uint):Array {
			var _array:Array = [];
			for (var i:uint = 0; i < n; i++) {
				_array[i] = i;
			}
			return _array;
		}
		private function createTextField():void {
			addChild(my_txt);
			addChild(label_txt);
			my_txt.x +=  30;
			my_txt.autoSize = TextFieldAutoSize.RIGHT;
			my_fmt.align = TextFormatAlign.RIGHT;
			my_txt.defaultTextFormat = my_fmt;
			label_txt.autoSize = TextFieldAutoSize.LEFT;
			label_txt.text = "";
			label_txt.appendText("apply() and push():\n");
			label_txt.appendText("concat():\n");
			label_txt.appendText("apply() and push():\n");
			label_txt.appendText("concat():\n");
			label_txt.appendText("apply() and push():\n");
			label_txt.appendText("concat():\n");
			label_txt.appendText("apply() and push():\n");
			label_txt.appendText("concat():\n");
		}
		private function xTrace(n:int):void {
			my_txt.appendText(String(n) + "\n");
		}
	}
}