Search code examples
performanceactionscriptreadability

Best way to join arrays in Actionscript


I need to add one array to another (only care about saving the joined one). Which is the preferred way of doing this? Speed is the primary concern followed by readability (I consider Option 1 to be a cleaner option). I assume it might also depend on the length of the arrays, but are there any good guidelines?

Option 1:

var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");

// Don't care about array2 after this point.
var array1 = array1.concat(array2);

Option 2:

var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");

// Don't care about array2 after this loop has run.
for each(var item:Object in array2)
{
    array1.push(item);
}

Solution

  • This sounds like a job for... benchmark!

    import flash.utils.getTimer;
    
    function addItems($array:Array, $start:int, $count:int) {
        for (var i:Number = $start; i < $start + $count; i++) {
            $array.push(i);
        }
    }
    
    function concatArrays($array1:Array, $array2:Array):Number {
        var t1:Number = getTimer();
        $array1.concat($array2);
        var t2:Number = getTimer();
        return t2 - t1;
    }
    
    function pushArrays($array1:Array, $array2:Array):Number {
        var t1:Number = getTimer();
        for each (var item:Object in $array2) {
            $array1.push(item);
        }
        var t2:Number = getTimer();
        return t2 - t1;
    }
    
    function testBed() {
        for (var i:Number = 10000; i <= 100000; i+=10000) {
            trace("\n---- New test");
            var a1:Array = [];
            var a2:Array = [];
            addItems(a1, 0, i);
            addItems(a2, i, i);
            trace("For " + a1.length + " items: ");
            trace("concatArrays: " + concatArrays(a1, a2));
            trace("pushArrays:   " + pushArrays(a1, a2));
        }
    }
    
    testBed();
    

    As I would have suspected, concat is far faster, especially when dealing with larger arrays.

    OUTPUT

    ---- New test
    For 10000 items: 
    concatArrays: 1
    pushArrays:   1
    
    ---- New test
    For 20000 items: 
    concatArrays: 1
    pushArrays:   4
    
    ---- New test
    For 30000 items: 
    concatArrays: 1
    pushArrays:   4
    
    ---- New test
    For 40000 items: 
    concatArrays: 2
    pushArrays:   5
    
    ---- New test
    For 50000 items: 
    concatArrays: 1
    pushArrays:   6
    
    ---- New test
    For 60000 items: 
    concatArrays: 1
    pushArrays:   7
    
    ---- New test
    For 70000 items: 
    concatArrays: 1
    pushArrays:   8
    
    ---- New test
    For 80000 items: 
    concatArrays: 2
    pushArrays:   12
    
    ---- New test
    For 90000 items: 
    concatArrays: 2
    pushArrays:   13
    
    ---- New test
    For 100000 items: 
    concatArrays: 3
    pushArrays:   14
    

    Those numbers are in milliseconds, so as Richard points out, unless your arrays have a huge number of elements, or you are concatenating arrays very frequently, then this is an optimization that is not worth your time