Myth Buster – Benchmarking Loops

Lets have a look at some of the most common and fastest loops used in ActionScript 3. Is int really faster than uint and Number? Is while loop faster than for? Is pre-increment faster than post-increment? To test all of these I have created benchmark test application on wonderfl. App compares loops with 10.000.000 iterations and logs testing times. To compare different variable types, loop methods, increment / decrement modes, there are buttons grouping each test. Please be patient with your first click, the model Array is being generated (Link button also generates linked list). The first test compares Arrays, second one Vectors.

Read the rest of this entry »

Common Tricks in ActionScript Code Shortening

Code shortening can make your code more readable as well as more unreadable :-). While it is recommend to use coding conventions and standards, in some cases you can spare a lot of lines using some shortening. In fact there must not be a conflict between using convetion vs. using shortening, and soon after you get used to some “shortcuts”, your code may be reduced in reasonable amount of lines. Some reading about code optimization can be found here, this article extends optimizations.

Read the rest of this entry »

Frame rate optimization

graph-going-up

There has been a lot of articles written about how to optimize your flash / flex application frameRate. In most cases, you only need to have full frameRate when some animation is playing, or tweening etc., in other words when some visual changes are going on. FrameRateOptimizator class does exactly what you need. In default, it uses full frameRate when visual changes and when it stops, optimizator automatically switches to lower frameRate. Optimizator uses bitmapData to compare last state with actual state to catch any visual changes. By default snapshot is executed two times per second in low frameRate (inactive state), or one time per second in full frameRate state (active state), taking approximately not more than 10ms per execution (fast enough).

Read the rest of this entry »

Quick tip: Array vs. Vector

Take a closer look at the ActionScript 3 memory consumption, when stored huge amount of values in Vector and Array. First list of uints:

(10000000 / 32) uints in a Vector (10000000 bits) = ~7k
(10000000 / 32) uints in an Array (10000000 bits) = ~1228k

… and list of Booleans:

10000000 Booleans in a Vector = ~8k
10000000 Booleans in an Array = ~39070k

Read more about AVM2 Memory Considerations.

Apparat (TAAS) framework to optimize swf

Joa Ebert is a Flash developer with a focus on ActionScript 3. He has developed several tools helping to optimize daily workflow. Most projects have been released as open-source. The most important one is Apparat / TAAS project:

Apparat is an open source framework written in Java to optimize ABC, SWC and SWF files. The core of Apparat are its three file formats and their implementation:

  • ABC (read and write ABC files, bytecode analysis and tools)
  • SWC (read and write SWC files)
  • SWF (read and write SWF files, tag implementations)

TAAS is a stackless representation of the ActionScriptBytecode. Read first results of TAAS, and see application before and after optimization.

Read the rest of this entry »

Code optimization

First, my golden rule: method with more than 12 rows is too long. Methods should be short, simple, clean and reusable. In most cases your methods fits into 5-12 rows. If not, you should consider rewriting your methods.

Look at the next code. There is too much nested tests. Soon you get lost with more and more nested levels:

function gimmeResult(a, b, c)
{
    var result;
    if(a)
    {
        if(b || c)
        {
            if(b && c < 0)
            {
                result = 1;
            }
            else
            {
                result = 2;
                // where am I?
            }
        }
        else
        {
            result = 3;
        }
    }
    else
    {
        result = 4;
    }
    return result;
}

Try to linearize code, it is much more cleaner for reading when no multiple nesting used:

function gimmeResult(a, b, c)
{
    if(!a)
        return 4;
    if(!b && !c)
        return 3;
    if(b && c < 0)
        return 1;
    return 2;
}

Read the rest of this entry »