PDA

View Full Version : index of array in for-each loop



nook
January 29th, 2009, 09:44 AM
This is a question spawned from my previous post (you don't have to read it):
http://www.kirupa.com/forum/showthread.php?p=2432200#post2432200

Consider an array with x number of elements (of type Object) where some of the elements might be undefined.
I'd like to use a for-each loop to access all the elements of the array that is not undefined (since it simply skips the undefined elements).
But. I also want the index of that particular element.
So I have two options:
1) Use array.indexOf(element).
2) Use traditional for loop, thus getting the index out of the iterator.

I'd rather not use option 2 since it contradicts the whole idea of not having to deal with undefined elements.
But I'm wondering if option 1 will be quick enough, a feels a bit like looking for water on the other side of the river so to speak.

Can you think of other solutions?

:: nook

cbeech
January 29th, 2009, 10:03 AM
hmmm interesting - looks like a 'six to one, half dozen to the other' deal here. I'm guessing (haven't tested) that the indexOf method will return pretty quick, so the question really is which is faster(?) the only way to find out would be to set up an array (large one would be best) and then run both while timing it to see.

I'm guessing (again ;) ) that the 1) method will be faster, since you're not iterating through each index, and i imagine the loop controling the method will have 'instant' access to the index property at that point in the operation.

wvxvw
January 29th, 2009, 10:39 AM
var arr:Array =[1, undefined, 2];
arr.forEach(foo);
function foo(element:Object, index:int, all:Array):void
{
trace(element, index);
}
In general forEach() is more slow then for-each loop. But if you're going to try that out anyway, you may try this too.

nook
January 29th, 2009, 11:17 AM
I just realise I've been mistaken.

If I create an array like this:
var a:Array = new Array();
a[0] = someValue;
a[2] = someValue;

Shure enough, the for-each loop will skip index 1.

But if index 1 have had a value, it seems I can't bypass it anymore by setting it to null or undefined.
The for-each loop will pick up that index showing the element as null.

That sort of ruins the whole concept.. :( or is it a way to do it?

senocular
January 29th, 2009, 11:24 AM
use delete

cbeech
January 29th, 2009, 12:15 PM
looks like iteration is the hands down winner, for .. each is not far behind, but forEach is way slow:




var a = [ ];
for(var b=0; b<1000; b++) { if(Math.random() < 0.5) a[b] = b; }

var stime:Number;
var etime:Number;

function time_iteration():void {
stime = getTimer();
for(var c=0; c<a.length; c++) { if(a[c] is int) trace(c+' : '+a[c]); }
etime = getTimer();

trace('iteration time: '+(etime - stime));
}

time_iteration();

function time_foreach():void {
stime = getTimer();
for(var d in a) { if(d is int) trace(a.indexOf(d)+' : '+d); }
etime = getTimer();

trace('foreach time: '+(etime - stime));
}

time_foreach();

function time_forEach():void {
stime = getTimer();
a.forEach(time_forEach);
function time_forEach(e:Object, i:int, ar:Array):void { trace(i+' : '+e); }
etime = getTimer();

trace('forEach time: '+(etime - stime));
}

time_forEach();

nook
January 29th, 2009, 01:15 PM
Great. But when I try I get approx.:
2ms
22ms
4ms

Ie for.. each seems to be the slow one. Did you mix them up?

cbeech
January 29th, 2009, 01:35 PM
WOW! you must have a great system LOL! mine were like (from memory):

232
254
548

i assumed that because the forEach method traces 'every' index - it made sense that it was the slowest.

omg - i just noticed that the nest method has the same name as the parent, should change that.

cbeech
January 29th, 2009, 01:42 PM
ran it again - got:

238, 238, 501

nook
January 29th, 2009, 01:43 PM
Could you post your latest code?

nook
January 29th, 2009, 02:18 PM
In case you haven't figured.. I'm Robo in adobe forums. ;)

cbeech
January 29th, 2009, 04:28 PM
LOL! yeah i noticed ;) i used the same as above, just changed the name of the nested function for the forEach method - i tried kg's suggestion and came up with similar results - bottom line, as he said as well is that iteration is the fastest methology, but an associative array construct can perform closely.

nook
January 30th, 2009, 06:08 AM
Cool. What about for..each? From my tests it seems to be the fastest of all when looping through an array.