‘with’ Statement Performance
Jul 01, 2009

Whilst reading a post from the Learning ActionScript 3.0 blog I noticed a tip which dealt with use of the ‘with’ statement. This reminded me of a post which I published on the old version of this site.

The ‘with’ statement can be used in your code to help make the code easier to read. Examples of how useful it can be can be found in the documentation.

I’ve never actually used the ‘with’ statement in any of my personal projects however after reading an article which mentioned it I decided to see whether or not there was a speed increase to be gained from using the ‘with’ statement. I created a small test which attempts to determine this which you can try out below.

How to use the test is self explanatory. When you start a test, 500 MovieClips are created. On every frame, the position, dimensions, alpha and rotation of each MovieClip is altered to a random value. You can view how long it takes for each frame to render in the dPanel by pressing the grave accent key (the ‘dPanel’ is a class I have created for debug purposes - I will make it available in the near future).

I presumed that the ‘with’ statement would be faster because there are less reference calls but you can see for yourself that the ‘with’ statement is actually a little slower.

With Statement Test source files.

2 Responses to “‘with’ Statement Performance”

  1. Jackson Dunstan says:
     

    Thanks for the great example. It’s also a great example of dPanel. I don’t find the with keyword used much, but I guess that’s a good thing!

  2. KrogothCN says:
     

    “with” statement is slower.and it will be very slow in following case.

    var A
    var B
    var C

    with(someObject)
    {
    var X=m1+A+m2+B+m3+C
    }

    i guess that flash player search “A” in someObject at first,if fail then search “A” in current object.this process spend a lot of time.

Add a Comment