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


Groups > comp.lang.javascript > #21314

Re: loop through an arry

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!newsfeed.fsmpi.rwth-aachen.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2406331.xsRxLlKTau@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 13 Sep 2013 03:17:32 +0200
User-Agent KNode/4.10.5
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: loop through an arry
Newsgroups comp.lang.javascript
References <l0tb6o$8a5$1@dont-email.me> <b7567773-4470-4827-b2a2-3e1057c303fe@googlegroups.com>
MIME-Version 1.0
Lines 64
NNTP-Posting-Date 13 Sep 2013 03:18:37 CEST
NNTP-Posting-Host c8bff5cb.newsspool4.arcor-online.net
X-Trace DXC=<ZhiLBgoD=JI7\_^6>c20J4IUK<Cl32<A4Fo<]lROoRA8kF<OcfhCOKQce:MKiA0>EDZm8W4\YJNLb@mF9jNikdEDZIgZChmYcKe_5N9?bj3`H
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:21314

Show key headers only | View raw


Adrián Espinosa wrote:

> On Thursday, September 12, 2013 11:19:59 PM UTC+2, 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++)
>> 
>>    total += theArray[i];
>> 
>> My alternative
>> 
>> I would have written this like this instead and I don't see any problem
>> with
>> 
>> that.
>> 
>> for(var i = 0; i  < theArray.length; i++)
>> 
>>    total += theArray[i];
>> 
>> My question I can't see any point to use the alternative that the book
>> suggest.
>> 
>> I prefer my alternatives.
> 
> On a moderated size array I think you wouldn't notice the difference, but
> on a larger one, in your alternative you are getting the length directly
> from the object, which has more overhead than getting the value from a
> stored one. I can't extend more on this, because I read it sometime ago.
> Maybe someone can clarify my explanation or tell us if I'm completely
> wrong.

We have discussed this here numerous times before.  You would be well-
advised to search any newsgroup archives that are available to you (Google 
Groups, being based on the DejaNews archive, still does a good job at that) 
before you post.  In summary:

If “theArray” refers to an Array instance, the size of the array does not 
matter.  At least for Array instances, the value of the “length” property is 
_not_ recalculated on read access, it is set on write access.  (As specified 
in ECMAScript Ed. 5.1, section 15.4, and in previous Editions, the “length” 
property of Array instances yields the index of the last element plus one, 
not necessarily the number of elements in the array.  This is different for 
NodeLists and HTMLCollections.)

What matters is the unnecessary repeated property access when the property 
value does not change.  As a result, the “book alternative” (which IIRC were 
suggested here almost a decade ago, perhaps even by me) is considerably 
faster.  The only caveat is that you have to be reasonably sure that the 
“length” of the array-like object does not change in-between.  This may not 
be true for Array instances for which there are publicly accessible 
references, and it may not be true for NodeLists and HTMLCollections.  If 
you cannot be sure of that, you should either handle the consequences, or 
use the slower approach.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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