I´m a javascript nooba, so thanks for the tip :D

On 5/23/07, Gordon <[EMAIL PROTECTED]> wrote:

It is one of those blindingly obvious things but also easy to forget.
I suppose the tip should be "if you find $ in a loop, find a way to
cache it".

On May 23, 4:43 pm, "Rob Desbois" <[EMAIL PROTECTED]> wrote:
> Absolutely, no loop should call a determinate function with the same
> argument(s) on every iteration.
> In other words, if a function is called in a loop and it will return the
> same value for each iteration take it OUT of the loop.
>
> Also Gordon, remember that no more memory is used here - in the slow version
> the result from .children() has to be cached somewhere in a temporary
> variable internally (I would guess, my knowledge of JS internals is
> minimal). Making the temporary variable explicit will use no additional
> memory.
>
> --rob
>
> On 5/23/07, Gordon <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > This one's probably blindingly obvious to anyone with any significant
> > programming experience in javascript, but it's an easy one to forget
> > too.
>
> > Consider this code:
>
> > function fast ()
> > {
> >         var myDivItems = $('#myDiv').children();
> >         for (x = 0; x < 1000; x++)
> >         {
> >                 myDivItems.length;
> >         }
> > }
> > function slow ()
> > {
> >         for (x = 0; x < 1000; x++)
> >         {
> >                 $('#myDiv').children().length;
> >         }
> > }
>
> > Both are functionally identical, but try profiling their run times in
> > Firebug!
>
> > Here's the average run times I got from running each function 5 times
>
> > fast: 3.125ms
> > slow: 3146.875ms
>
> > One more line of code, a bit more memory used, but huge win in loops.
>
> --
> Rob Desbois
> Eml: [EMAIL PROTECTED]
> Tel: 01452 760631
> Mob: 07946 705987
> "There's a whale there's a whale there's a whale fish" he cried, and the
> whale was in full view.
> ...Then ooh welcome. Ahhh. Ooh mug welcome.




--

[]´s Jean
www.suissa.info

  Ethereal Agency
www.etherealagency.com

Reply via email to