A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: [help] Which is faster, Arrays or Objects?

  1. #1
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168

    [help] Which is faster, Arrays or Objects?

    Hey all.

    I was just wondering about the speed differences between Arrays and Objects. If I have a chunk of data with many different properties (up to 10) and some of them containing larger amounts of data (such as paragraphs of text), would having an Object for each chunk be faster than having a multidimensional Array with each entry a sub Array with each bit of data in it. And what if I have only around 4 properties, just an x,y and a couple Boolean values, would the Array be faster? Just wondering if anyone knew off the top of their head (that expression looks very strange when you type it out ) Thanks!

    Dave
    DaveWasmer.com
    Programmers don't die, they just GOSUB without RETURN.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    I think objects act faster, but I havent run any tests to prove it.

  3. #3
    Member
    Join Date
    Mar 2000
    Location
    Atlanta
    Posts
    83
    I think [for...in] with objects is the fastest way to loop through data.

    brad

  4. #4
    for the win Asclepeos's Avatar
    Join Date
    Dec 2000
    Posts
    388
    array has order. for..in does not.

  5. #5
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168
    Thanks for your replies everyone, I will probably use arrays for the smaller amounts of info, and objects for the larger. Asclepeos has a good point to, looping through an array is easier to think through than a for...in loop and objects (for me at least )

    Thanks
    Dave
    DaveWasmer.com
    Programmers don't die, they just GOSUB without RETURN.

  6. #6
    Member
    Join Date
    Mar 2000
    Location
    Atlanta
    Posts
    83
    Thats what I get for listening to others. I have read about 10 places that for..in is fastest. Here are some tests.

    Paste this in and take a look:


    function test(){
    var st = getTimer();

    var len = test1.length;
    for(var i=0; i<len;i++){
    var myVar = test1[i];
    }

    var et = getTimer() - st;
    trace("Array time: " + et + " ms");
    //
    var st = getTimer();
    var len = test1.length;
    for(var i = len; i>0;i--){
    var myVar = test1[i];
    }

    var et = getTimer() - st;
    trace("Reverse Array time: " + et + " ms");
    trace("");
    //
    var st = getTimer();
    for(var i in test2){
    var myVar = test2[i];
    }

    var et = getTimer() - st;
    trace("For in Object time: " + et + " ms");
    //
    var st = getTimer();
    for(var i=0;i<len;i++){
    var myVar = test2[i];
    }

    var et = getTimer() - st;
    trace("Loop Object time: " + et + " ms");
    //
    var st = getTimer();
    for(var i=len;i>0;i--){
    var myVar = test2[i];
    }

    var et = getTimer() - st;
    trace("Reverse Loop Object time: " + et + " ms");
    //
    var st = getTimer();
    for(var i in test3){
    var myVar = test3[i];
    }

    var et = getTimer() - st;
    trace("For Object in Object time: " + et + " ms");
    trace("");
    //
    var st = getTimer();
    while(len--){
    var myVar = test1[i];
    }

    var et = getTimer() - st;
    trace("While w Array time: " + et + " ms");
    //
    var len = test1.length;
    var st = getTimer();
    while(len--){
    var myVar = test2[i];
    }

    var et = getTimer() - st;
    trace("While w Object time: " + et + " ms");

    clearInterval(wait);
    }

    test1 = new Array();
    test2 = new Object()
    test3 = new Object();

    trace("Store data then wait a couple seconds...");
    for(var i=0;i<50000;i++){
    test1.push(random(99999));
    test2[i] = random(99999);
    test3[i] = new Object();
    }

    wait = setInterval(test, 2000);

  7. #7
    Member
    Join Date
    Mar 2000
    Location
    Atlanta
    Posts
    83
    Yeah so anyway, that output:

    Store data then wait a couple seconds...
    Array time: 220 ms
    Reverse Array time: 222 ms

    For in Object time: 1605 ms
    Loop Object time: 220 ms
    Reverse Loop Object time: 220 ms
    For Object in Object time: 2188 ms

    While w Array time: 173 ms
    While w Object time: 165 ms

    So it looks like while{} with data in Objects is the fastest. With arrays being a close second and for...in being the slowest thing ever.

    brad

  8. #8
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    I just did a test myself.

    Im going to punch the guy in the face that said for(in) for stepping through an array was faster.. Its so much slower..

    (for a 10000 element array, 70ms vs 120ms)

    its not much tho, its quite a small gain in speed.
    Christopher Rhodes
    squarecircleco.

  9. #9
    for the win Asclepeos's Avatar
    Join Date
    Dec 2000
    Posts
    388
    I personally don't think it matters that much.. all this nitpicking in speed. I assumed for..in was faster in small arrays and iterative for loops were faster in large arrays. I disregard speed for a little organization and 'carmackian' philosophy of iterating only through the loops that matter.

  10. #10
    Member
    Join Date
    Mar 2000
    Location
    Atlanta
    Posts
    83
    Another interesting note. I was just looking at the code in AS Viewer for my SWF and for(){} tranlates into a while{} with extra stuff. So...

    for(var i=0; i<20; i++){
    //stuff
    }

    translates to:

    while(i<20){
    //stuff
    i++
    }

    brad

  11. #11
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168
    Okay, thanks for the advice. I wasn't really nitpicking about speed, I just had no idea if the Arrays/Objects were going to be 10x faster of .1x faster. Thanks
    Programmers don't die, they just GOSUB without RETURN.

  12. #12
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    712
    here are the results of the above test only I changed 50000 to 2000, because it causes my script to stop:

    Store data then wait a couple seconds...
    Array time: 129 ms
    Reverse Array time: 115 ms

    For in Object time: 94 ms
    Loop Object time: 132 ms
    Reverse Loop Object time: 120 ms
    For Object in Object time: 94 ms

    While w Array time: 104 ms
    While w Object time: 104 ms

    I wonder how objective these flash tests are? They could depend on a whole number of issues concerning the computer and such. I'm running a 677Mhz pentium III on win NT, btw.

  13. #13
    Cubed Member Soccr743's Avatar
    Join Date
    Mar 2004
    Location
    Maryland
    Posts
    163
    Pentium 4 3.09 GHZ on Windows XP

    Using 50000:

    Store data then wait a couple seconds...
    Array time: 143 ms
    Reverse Array time: 144 ms

    For in Object time: 443 ms
    Loop Object time: 138 ms
    Reverse Loop Object time: 139 ms
    For Object in Object time: 568 ms

    While w Array time: 98 ms
    While w Object time: 98 ms


    Using 100000:

    Store data then wait a couple seconds...
    Array time: 289 ms
    Reverse Array time: 292 ms

    For in Object time: 934 ms
    Loop Object time: 289 ms
    Reverse Loop Object time: 287 ms
    For Object in Object time: 2147 ms

    While w Array time: 205 ms
    While w Object time: 214 ms


    -----Soccr743-----

  14. #14
    Member
    Join Date
    Mar 2000
    Location
    Atlanta
    Posts
    83
    here are the results of the above test only I changed 50000 to 2000, because it causes my script to stop:
    Array time: 129 ms
    Reverse Array time: 115 ms
    For in Object time: 94 ms
    Wow, thats weird. You are showing [For...in] is faster. At 2000 loops I get:
    Array time: 8 ms
    Reverse Array time: 9 ms
    For in Object time: 24 ms

    So weird people get such radically different results.

    brad

  15. #15
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    712
    That's why I'm questioning the objectivity of these tests. Because on different computer you conclude different things from them. Maybe it's because I have most keyboard shortcuts disabled when viewing the movie; I have no idea why I got those results.

    Edit: I keep the getting the same results, actually, still.
    Last edited by Kirill M.; 04-12-2004 at 10:30 AM.

  16. #16
    for the win Asclepeos's Avatar
    Join Date
    Dec 2000
    Posts
    388
    different object complexity?.

  17. #17
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    712
    He just ran it with 2000 entries in each thing, like I did, and from his results for in isn't good, while from my results for the same number of entries for in is good.

  18. #18
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    712
    The swf has 5000 entries. This way the factors involved in making the swf should be gone.
    Attached Files Attached Files

  19. #19
    Ihoss
    Guest
    Array time: 135 ms
    Reverse Array time: 131 ms

    For in Object time: 83 ms
    Loop Object time: 142 ms
    Reverse Loop Object time: 136 ms
    For Object in Object time: 88 ms

    While w Array time: 86 ms
    While w Object time: 75 ms

    This is very important for my new game where i can save almost a second on this stuff!

  20. #20
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,651
    Array time: 1077 ms
    Reverse Array time: 992 ms

    For in Object time: 917 ms
    Loop Object time: 1031 ms
    Reverse Loop Object time: 939 ms
    For Object in Object time: 894 ms

    While w Array time: 843 ms
    While w Object time: 857 ms

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center