Initializing Arrays
/**
* Copyright Fumio ( http://wonderfl.net/user/Fumio )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/leRa
*/
package {
import flash.display.Sprite;
import flash.utils.getTimer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
[SWF(width = "240",height = "180")]
public class InitializingArrays extends Sprite {
private const MAX_NUMBER:int = 1000000;
private var i:int;
private var started:int;
private var my_txt:TextField = new TextField();
private var label_txt:TextField = new TextField();
public function InitializingArrays() {
// Creating a TextField for display
createTextField();
// Starting test
newOperator();
bracketAccessOperator();
lengthToZero();
}
private function newOperator():void {
started = getTimer();
for (i = 0; i < MAX_NUMBER; i++) {
var my_array:Array = new Array();
my_array[0] = 1;
}
xTrace(getTimer() - started);
}
private function bracketAccessOperator():void {
started = getTimer();
for (i = 0; i < MAX_NUMBER; i++) {
var my_array:Array = [];
my_array[0] = 1;
}
xTrace(getTimer() - started);
}
private function lengthToZero():void {
started = getTimer();
var my_array:Array = new Array();
for (i = 0; i < MAX_NUMBER; i++) {
my_array.length = 0;
my_array[0] = 1;
}
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 = "new:\n[]:\nlength:";
}
private function xTrace(n:int):void {
my_txt.appendText(String(n) + "\n");
}
}
}