Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21307

Re: loop through an arry

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!aioe.org!.POSTED!not-for-mail
From Ken <usenet-so@abdiassoftware.com>
Newsgroups comp.lang.javascript
Subject Re: loop through an arry
Date Thu, 12 Sep 2013 16:35:45 -0500
Organization Abdias Software
Lines 32
Message-ID <l0tc62$3pr$1@speranza.aioe.org> (permalink)
References <l0tb6o$8a5$1@dont-email.me>
NNTP-Posting-Host gsV+T5Vt2EZrXzSXRCvaFw.user.speranza.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.javascript:21307

Show key headers only | View raw


On 12.09.2013 16:19, Tony Johansson wrote:> I just wonder because I have 
seen this in a book
 >
 > The book alternative
 > var length = theArray.length;  //get the array's length one before loop
 > for(var i = 0; i  < length; i++)
 >
 > My alternative
 > for(var i = 0; i  < theArray.length; i++)
 >
 > My question I can't see any point to use the alternative that the book
 > suggest.
 > I prefer my alternatives.

It executes faster as it caches the length value to a local scoped 
variable. Othwerwise, for each iteration (related to being a dynamic 
language), the interpreter needs to first lookup theArray and then 
length from theArray. By caching the value to a variable first it only 
needs to look up that variable.

However, this was more a "noticeable" thing in the past. Current 
browsers are capable of optmizing this pretty good and unless you really 
need to squeeze out all you can of the browser (which in case you would 
use a *while* loop in any case) there is no real problem using your 
approach.


-- 
Ken Fyrstenberg
Abdias Software
http://abdiassoftware.com/
(remove usenet- from address)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

loop through an arry "Tony Johansson" <johansson.andersson@telia.com> - 2013-09-12 23:19 +0200
  Re: loop through an arry Adrián Espinosa <mortuuslordofdeads@gmail.com> - 2013-09-12 14:30 -0700
    Re: loop through an arry Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-13 03:17 +0200
      Re: loop through an arry Stefan Weiss <krewecherl@gmail.com> - 2013-09-13 04:27 +0200
        Re: loop through an arry Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-13 05:23 +0200
          Re: loop through an arry Stefan Weiss <krewecherl@gmail.com> - 2013-09-13 05:36 +0200
      Re: loop through an arry "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-13 09:48 +0200
  Re: loop through an arry Ken <usenet-so@abdiassoftware.com> - 2013-09-12 16:35 -0500
    Re: loop through an arry Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-09-12 16:39 -0700
      Re: loop through an arry Ken <usenet-so@abdiassoftware.com> - 2013-09-12 19:45 -0500
        Re: loop through an arry Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-09-13 11:18 -0700
          Re: loop through an arry Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-14 01:49 +0200
            Re: loop through an arry Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-09-13 17:06 -0700

csiph-web