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


Groups > comp.lang.javascript > #21316

Re: loop through an arry

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: loop through an arry
Date 2013-09-13 04:27 +0200
Organization albasani.net
Message-ID <l0tt67$ge1$1@news.albasani.net> (permalink)
References <l0tb6o$8a5$1@dont-email.me> <b7567773-4470-4827-b2a2-3e1057c303fe@googlegroups.com> <2406331.xsRxLlKTau@PointedEars.de>

Show all headers | View raw


On 2013-09-13 03:17, Thomas 'PointedEars' Lahn wrote:
> 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.

Caveats about changing array lengths during the course of a loop aside,
what matters most is the implementation. Practically all modern JS
engines contain optimizations for the uncached alternative. In some rare
cases, they even test faster when array.length is (supposedly) checked
on each iteration. There are hundreds of tests about this on jsperf.com,
if you're curious.

But we cannot be sure that the client engine - or the host object we're
iterating over - is sufficiently optimized in all cases. Caching the
array length in a variable doesn't hurt (performance or readability), so
I usually go for the "book alternative", too.

If the expected length of the array is lower than, say, 100, it hardly
matters anyway. Use whatever style suits you.

- stefan

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