Adding elements to an Array
/**
* Copyright Fumio ( http://wonderfl.net/user/Fumio )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/i094
*/
package {
import flash.display.Sprite;
import flash.utils.getTimer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
[SWF(width = "240",height = "180")]
public class AddingElementsToArray extends Sprite {
private const MAX_NUMBER:int = 10000000;
private var i:int;
private var started:int;
private var my_txt:TextField = new TextField();
private var label_txt:TextField = new TextField();
public function AddingElementsToArray() {
// Creating a TextField for display
createTextField();
// Starting test
pushMethod();
bracketAccessOperator();
}
private function pushMethod():void {
started = getTimer();
var my_array:Array = new Array();
for (i = 0; i < MAX_NUMBER; i++) {
my_array.push(i);
}
xTrace(getTimer() - started);
}
private function bracketAccessOperator():void {
started = getTimer();
var my_array:Array = [];
for (i = 0; i < MAX_NUMBER; i++) {
my_array[my_array.length] = i;
}
xTrace(getTimer() - started);
}
private function createTextField():void {
addChild(my_txt);
addChild(label_txt);
my_txt.autoSize = TextFieldAutoSize.RIGHT;
label_txt.autoSize = TextFieldAutoSize.LEFT;
label_txt.text = "push():\n[length]:";
}
private function xTrace(n:int):void {
my_txt.appendText(String(n) + "\n");
}
}
}