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.
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.
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();
}
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.
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.
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.
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.
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.
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.