Actionscript3 tip: faster property initializing

I was looking for a way to set parameters more quickly to newly added childs and I found out something interesting. (Note that this will not result in faster code execution, it’s merely to type less :-) . I did some testing for the performance, check it out at the bottom of the page.)

Normally when I create a new DisplayObject (sprite, mc, …) that I want to position right after I do it like this:

var mc:MovieClip = new MovieClip();
mc.x = 200;
mc.y = 300;
mc.name = "myTestClip_mc";
...
addChild(mc);

Now this can be shorter right? Creating a temp variable is not a bad practice but I like to do this in an even shorter way. (I remember the initObject parameter of the attachMovieClip function in as2 which was a pretty useful feature.)
Now I already knew you could use the with keyword to set vars more quickly on any object. Unfortunately you loose code completion/intellisense :-(

var mc:MovieClip = new MovieClip();
with(mc)
{
x = 200;
y = 300;
name = "myTestClip_mc";
...
}
addChild(mc);

But still, I need a reference to the object in order to add it to the displaylist afterwards.
Now when browsing the docs I saw that the addChild function returns the element you’ve just added, which is not something to be surprised about, we had this already with createNewMovieClip/AttachMovieClip in as2, but it just didn’t cross my mind to use it that way.

addChild(child:DisplayObject):DisplayObject

So if you combine those two things, the original code can be easily formatted to the following format, which is exactly what I was looking for, short code without creating temporary variables:

with(addChild(new MovieClip()))
{
x = 200;
y = 300;
name = "myTestClip_mc";
...
}

So after I got some feedback I decided to make a small test about the subject. The testfile has 3 functions, test with the method mentioned above, a test with temp vars where I add the child after setting the x/y variables. I added a version where a mc is loaded from the library and a version using a sprite in combination with the drawing api.

Go ahead and try it out! (source)

Notice that there is not that much of a difference in execution time with few iterations. But if you increase the number of iterations, the differences becomes clear pretty fast (tested in ff3 under windows xp, 2.66 ghz, 3 gb ram)

Share
back

2 comments

  1. Romu says:

    Be careful with that though, this proves to be much slower:
    var myClass:MyClass = addChild(new MyClass()) as MyClass;
    This will be faster:
    var myClass:MyClass = MyClass(addChild(new MyClass()));
    But still slower than a normal instantiation line by line.
    Make a test instantiating 200 sprite for example, you’ll be surprised.

    Romu

  2. Alwyn says:

    Yes I should have mentioned that I meaned faster typing. It’s not necessarily faster execution :-) .
    In regards to your comment, MyClass(addChild(new MyClass())) is indeed faster but it will throw an error in the debugger if the casting fails. addChild(new MyClass()) as MyClass on the other hand will not.

Leave a Reply

Your email address will not be published. Required fields are marked *

*