Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: [ NOT A BUG ] View.as for() loops  (Read 8627 times)
paddy
Newbie
*
Posts: 8


View Profile Email
« on: September 23, 2009, 07:08:00 »

hi Cliff,

This isn't a bug but after attending Grant Skinners talk at FOTB. There are some small optimisations you could make to the View.as

:
for (var i:Number = 0; i < observers_ref.length; i++) {
observer = observers_ref[ i ] as IObserver;
observers.push( observer );
}

to

:
var len:int = observers_ref.length
for (var i:int = 0; i < len; ++i) {
observer = observers_ref[ i ] as IObserver;
observers.push( observer );
}

i.e using int instead of Number, moving the length calculation out of loop, using ++i instead of ++i

could go a little way to helping with large apps... Hope all's well, thanks paddy ;)


« Last Edit: September 24, 2009, 07:37:29 by puremvc » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: September 24, 2009, 07:36:47 »

length is a property of the observers_ref array which is recalculated by the array whenever items are added or removed (which isn't happening here). Thus our reference to observers_ref.length in the loop condition does no calculation whatsoever so putting it in a separate variable reference before the loop doesn't do anything for us.

And we don't want to use ++i instead of i++ because we don't want to skip the first pass through the loop where i == 0;

-=Cliff>
Logged
paddy
Newbie
*
Posts: 8


View Profile Email
« Reply #2 on: September 25, 2009, 10:10:12 »

hi cliff,

That's what I though but ++i doesn't miss first (or last) loop and apprently is slightly more optimal?

:
for (var i:int = 0; i < 10; ++i) {
trace(i)
}

also apprently using int instead of Number in loops has benifical results (perticually for Flash 10):
http://gskinner.com/talks/quick/#10

we're prob only talking about small loops here so this will may not have an effect but wanted to mention it after the talk ;)
Logged
Pages: [1]
Print