[SOLVED] Comparison between Array.forEach() & for each() & for (i)


(Bora Kasap) #1

how can i get max value of a public number property in every entity in an array? example, i have 3 instances of same entity, all of them have “width” value, and all of them in an array already but i’m looking for the “max” width value

list:Array = [entity1, entity2, entity3];
largestwidth:Number = Math.max(list[0].width, list[1].width, list[2].width)

what if i don’t know how many entities in that array? i need something like that

largestwidth:Number = Math.max(list.forEach(width));

but i don’t know how to use that callback stuff here?

i don’t wanna use another loop function instead of this Array.forEach stuff, i wanna learn how to use that


(Ultima2876) #2

The Array ‘forEach’ thing is slow. Math.min and Math.max… are very slow. On top of that, forEach isn’t really made for that kind of thing and would be WAY overkill for something like that! :smile:

You’d be much better just doing it the simple and obvious way:

var greatest: Number = 0;
for each(var e: Entity in list) { if(e != null && e.width > greatest) { greatest = e.width; } }
//greatest is now the greatest width.

Anyway, if you really do want to use the Array forEach thingy, the as3 docs has a better explanation of it than I can give!

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#forEach()

It’s not really designed for simple ops like getting the max value, but you could do it by defining a callback function that checks the current element’s width against a class variable storing the greatest width. It’d essentially be a roundabout, bloated way of doing the above simple loop!


(Bora Kasap) #3

Ahhhh!!! I’ve just realized god damn great thing!

Is that because of “for each” trying to match class types right??, it can be useful in arrays have different object types but no need for vector-like arrays…

Oh thats great tip, then i’m going to change all of my “for each” stuff to “for i” right now =))) hahaha

so, i’m going to change my “for each element:XML in someXMLList” stuff to “for i” stuff, am i able to get length of any XMLList?


(Ultima2876) #4

Some containers don’t let you get the length (mainly Dictionaries and POOs iirc). However, when you can get it (ie from an Array, Vector or List) it’ll be the length property.

There’s not an overwhelmingly massive difference between for each and regular for loops in terms of performance. Not anywhere near as much as between for each and Array.forEach() in any case!

Two principles apply; Keep It Simple Stupid and No Premature Optimization! In AS3 particularly, the simpler solution is often the best and if for each makes for more readable code I’d stick with that.

An example, a few years ago I was getting bad lag in one of my games. I thought it was my random number generation, which I was doing hundreds to thousands of times per frame and was a LOT more inefficient than it could have been (allocating obects, lots of vars, complex maths etc). I spent a whole working day (6 hours) updating it and replacing all of my “new RandomNumber” stuff with a static version. I gained… nothing; my game ran exactly the same as before, not a single frame per second difference. Turned out that switching to using PreRotations was what I needed to do and my problem had nothing to do with Random Numbers at all!

Moral of the story… be careful of micro-optimisation, especially sweeping generalizations like replacing all of your for each loops with for loops.