2

This question is really twofold:

  1. How does for each looping over an Array compare performance-wise with a simple for loop through its elements?

  2. Does a loop guarantee in order traversal? The following code says yes:

var sample_array:Array = [];
for (var i:uint = 0; i < 10000; i++) sample_array.push(i);
i = 0;
for each(var value:uint in sample_array) {
  sample_array[i++] = value;
}

trace('in order was:', check_in_order(sample_array));

function check_in_order(array:Array):Boolean
{
  for (var i:uint = 0, l:uint = array.length; i < l; ++i) {
    if (array[i] != i) return false;
  }

  return true;
}

but I have heard other (senior-level) engineers swear up and down that the traversal does not always proceed in ascending order! Is this true?

Sensei James
  • 2,617
  • 30
  • 36
  • 1
    http://stackoverflow.com/questions/1010859/for-vs-foreach-on-array-performance-in-as3-flex and http://stackoverflow.com/questions/618966/for-each-loop-as3-is-the-direction-guaranteed might be worth a read. – Joetjah Apr 10 '13 at 08:41
  • 1
    Take a look at http://jacksondunstan.com/articles/1978 too. It's a great article by Jackson Dunstan comparing for, for-in and for-each. – Fernando Bevilacqua Apr 10 '13 at 16:47
  • @Dovyski - post that as an answer and I'll accept it; dude does a great job of benchmarking (only thing that is perhaps lacking is inclusion of an Array of variable size). – Sensei James May 29 '13 at 06:31
  • @Joetjah - thanks for that. Re. the "for each guaranteed in order?" - Jon Skeet got militant with it! Right to the AS3 to the ECMA documentation, which neither confirms nor denies in order traversal. In Mr. Skeet's words - "It's a bit unsatisfactory, to be honest :(" I've never seen for each Array traversal be out or order in the tests I've run, for what it's worth. – Sensei James May 29 '13 at 06:41
  • @Sensei James I've posted my answer :) About what you said, I agree: the only thing that is lacking is an Array of variable size. However I don't believe it could hurt the performance during *read* operations. – Fernando Bevilacqua May 31 '13 at 03:55

3 Answers3

6

As demonstrated by Jackson Dunstan, who compared for, for each and for in with different collections (arrays, vectors, etc), the performance ranking is (from fastest to slowest):

  1. for
  2. for each
  3. for in (extremely slow)

In all cases for is the fastest of all. In particular when used with arrays or vectors, for has an overwhelming performance. Those are the numbers for all collections (smaller means faster):

Loop Speed Redux by Jackson Dunstan

1

here is fixed post from Adrian

var size:Number = 10000000;
var arr:Array = [];
var time:Number, o:Object, i:int;
// fill container
for (i = 0; i < size; i++) arr[i] = i;

// for i
time = getTimer();
for (i=0; i<arr.length; i++){ arr[i]; }
trace("for test: "+(getTimer()-time)+"ms");

// for i reversed
time = getTimer();
for (i=arr.length; i>0; i--) { arr[i]; }
trace("for reversed test: "+(getTimer()-time)+"ms");

// for each
time = getTimer();
for each(o in arr) { o; }
trace("for each test: "+(getTimer()-time)+"ms");

// Result
for test: 212ms
for reversed test: 26ms
for each test: 103ms
Orien
  • 47
  • 2
  • 7
  • 1
    `For in` here is actually `For each in`. Added `For in` and got this result: for test: 818ms [trace] for reversed test: 418ms [trace] for in test: 224ms [trace] for each in test: 168ms – Sergey Senkov Jan 10 '22 at 15:36
  • thx already fixed comments ;-) – Orien Jan 12 '22 at 17:37
-1

Please check my post Flex Array Performance: For vs. ForEach

Some code

var size:Number = 10000000;
var arr:Array = [];
for (var i:int=0; i
var time:Number, o:Object;

// for()
time = getTimer();
for (i=0; i=0; i--) { arr[i]; }
trace("for reversed test: "+(getTimer()-time)+"ms");

// for..in
time = getTimer();
for each(o in arr) { o; }
trace("for each test: "+(getTimer()-time)+"ms");

Results

for test: 124ms
for reversed test: 110ms
for each test: 261ms
Adrian Pirvulescu
  • 4,308
  • 3
  • 30
  • 47