0

I'm wondering which is faster in AS3:

array.forEach( function(v:Object, ...args):void
{ ... } );

Or

var l:int = array.length;
for ( var i:int = 0; i < l; i++ ) { ... }
0

4 Answers 4

6

for i :)

var array:Array = [];
for (var i:int=0; i < 100000; i++)
{
    array[i] = i;
}
var time:uint = getTimer();
array.forEach( function(v:Object, ...args):void
                { v = 1; } );
trace(getTimer()-time); //trace 85


time = getTimer();
var l:int = array.length;
for ( i = 0; i < l; i++ ) { array[i] = 0; }
trace(getTimer()-time); //trace 3
Sign up to request clarification or add additional context in comments.

Comments

3

The above answers do not take into account that you mostly will be performing operations on the array elements in the loop

This code

import flash.utils.getTimer;

var a:Array=[];
var time:int=0;

for(var i:int=0; i<10000; i++) {
    a.push(new Date());
}

time=getTimer();
for(var j:int=0; j<a.length; j++) {
    var dt:Date=a[j];
    dt.valueOf();
}
trace("for: " , getTimer()-time);

time=getTimer();
for each(var xt:Date in a) {
    xt.valueOf();
}
trace("for each: " , getTimer()-time);

time=getTimer();
a.forEach(nothing);
trace("a.forEach: " , getTimer()-time);

function nothing(d:Date, ...args):void {
    d.valueOf();
}

Returns the following:

for:  3
for each:  2
a.forEach:  13

For 100000 values, the results are starker still

for:  27
for each:  17
a.forEach:  138

Overall winner: the for each loop

for each(var d:myClass in myCollection) {
    //myCode
}

Comments

1

For VS Foreach on Array performance (in AS3/Flex)

hope this will help you in understanding the difference between for and for-each loop.

1 Comment

This question is about Array.forEach() vs. for. The question you mention is about for each vs for.
1
        var time:uint;
        var vec:Vector.<Number> = new Vector.<Number>;
        for (var b:int=0; b < 1000000; b++)
        {
            vec[b] = 99;
        }

        ///

        time = getTimer();
        for (var i:int = 0; i < vec.length; i++ )
        {
            vec[i] = 2;
        }
        trace('for i: '+(getTimer()-time)+'ms');

        ///

        time = getTimer();          
        for (var j:int = vec.length - 1; j >= 0; --j) 
        {
            vec[j] = 3;
        }
        trace('for i rev: '+(getTimer()-time)+'ms');            

        ///

        time = getTimer();
        for ( var k:int in vec)
        {
            vec[k] = 4;
        }
        trace('for: '+(getTimer()-time)+'ms');          

        ///

        time = getTimer();
        for each(var o:Number in vec)
        {
            o = 5;
        }
        trace('for each: '+(getTimer()-time)+'ms');         

        ///

        time = getTimer();
        vec.forEach( function(v:int, ...args):void
        {
            v = 6;
        }
        );
        trace('forEach: '+(getTimer()-time)+'ms');  

// for i: 81ms
// for i rev: 79ms
// for: 28ms
// for each: 33ms
// forEach: 530ms

3 Comments

Great that you took the time to answer! Can you edit your answer to include an explanation why you posted it? The question already has an accepted answer.
amoebe, from time to time adobe make changes in the performance of the as3 virtual machine, so that the information above can be no longer relevant. Does this explanation sounds good to you?
It's not like you have to defend yourself, I just wanted to help you creating better answers. Thank you for your contribution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.