var start:Number = getTimer();
for(var i:int=0;i<1000;i++){
// statements to evaluate
}
trace(start-getTimer());
if(var1) if(var2) if(var3)
is faster than
if(var1 && var2 && var3)
var list:Array = [1,2,3];
is faster than
var list:Array = new Array(1,2,3);
for(var i:int=0;i<1000;i++)
is faster than
var i:int=0;
while(i<1000)
prop in Object
is faster than
Object.hasOwnProperty("prop")
var var1:Number = container.child.x;
textfield.text = var1;
container.child2.x = var1;
is faster than
textfield.text = container.child.x;
container.child2.x = container.child.x;
public class MyObject{
public var variables:Object = {};
}
is faster than
public dynamic class MyObject{
}
var parts:Array = longString.split("|");
is faster than
var parts:Array = longString.match(/(.*?)\|/gm);
MyArray.push(myVar);
is faster than
MyArray.shift(myVar);
addEventListener("enterFrame",myFunc);
is faster than
myTimer.addEventListener("timer",myFunc);
function getMinutes(){
var myVar:uint = 6000;
return getTimer() / myVar;
}
is faster than
var myVar:uint = 6000;
function getMinutes(){
return getTimer() / myVar;
}
const URL:String = "http://www.experts-exchange.com";
is faster than
var URL:String = "http://www.experts-exchange.com";
var child:MyClass = event.currentTarget as MyClass;
is faster than
var child:MyClass = MyClass(event.currentTarget);
for each(var node:XML in myXML){
if(node.@myProp == "value"){
if(node.@anotherProp == 12){
react();
}
}
}
is faster than
var nodes:XMLList = myXML.(@myProp=="value").(@anotherProp==12);
for each(var node:XML in nodes){
react();
}
for(var i:int=0;i<10000;i++)
is faster than
for(var i:Number=0;i<10000;i++)
var n:Number = 93745298347.230498;
var m:Number = n|0;
is faster than
var n:Number = 93745298347.230498;
var m:Number = Math.foor(n);
var myArray:Array = [];
for(var i:int=0;i<1000;i++){
myArray[i] = "text";
};
var myString:String = myArray.join(" ");
is faster than
var myString:String = "";
for(var i:int=0;i<1000;i++){
myString += "text ";
};
var l:uint = myArray.length;
for(var i:int=0;i<l;i++)
is faster than
for(var i:int=0;i<myArray.length;i++)
private static function myFunc():void{};
is faster than
private function myFunc():void{};
var a:Number = 987234.230948;
is NOT faster than
var myLongVariableNameUsedToMatterButDoesntAnymore:Number = 987234.230948;
var n:Number;
for(var i:int=0;i<10000;i++){
n = Math.random();
}
is NOT faster than
for(var i:int=0;i<10000;i++){
var n:Number = Math.random();
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (11)
Commented:
I've got to question #13, though, simply because I've seen an Adobe engineer say the exact opposite:
http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=3#datatyping . (Scroll down a little bit to the "casting" subsection.)
I'm guessing your suggestion off of your empirical testing? I've never seen senocular be wrong, but I suppose there could always be a first time.
Aside from that issue - excellent ideas!
Author
Commented:Thanks for posting - I assume you mean #14 (which is about casting) not #13 (which is about "const" vs "var"). if so, we can easily determine which is faster with a simple test like the once I posted at the beginning of this article:
var bob:Object = new Sprite();
var start:Number = getTimer();
for(var i:int=0;i<500000;i++){
// var ken:Sprite = Sprite(bob);
var ken:Sprite = bob as Sprite;
}
trace(getTimer()-start);
casting using the first method comes back about 112 ms on my laptop.
casting using "as" comes back at about 50.
so casting with "as" is more than twice as fast as casting via constructor.
as far as the link you provided, I think he's saying casting with "as" is slower than not casting at all. if you can avoid casting to begin with, yes that would definitely be preferable, but sometimes it's necessary - often in the case of event.target/currentTarget
function whatever(event:Event):void
event.target.myMethod(); // results in failure
var ref:MySprite = event.target as MySprite;
ref.myMethod(); // works
}
if you can avoid that situation, and skip casting entirely by virtue of typing, then by all means do so:
var ref:MySprite = new MySprite();
ref.myMethod(); // works, avoids casting, and is preferrable - but not always possible
anyway, thanks again for the vote and your comment
Commented:
Author
Commented:Commented:
Open in new window
gives me impression that this is dumb AS$ :PView More