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


Groups > comp.lang.javascript > #21335

Re: loop through an arry

Path csiph.com!usenet.pasdenom.info!poup.poupinou.org!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1553168.V9fml2jhPD@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Sat, 14 Sep 2013 01:49:11 +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> <l0tc62$3pr$1@speranza.aioe.org> <GgsYt.30492$VU.28941@fx05.iad> <l0tnar$rnh$1@speranza.aioe.org> <PFIYt.44777$dJ2.1784@fx30.iad>
MIME-Version 1.0
Lines 78
NNTP-Posting-Date 14 Sep 2013 01:49:15 CEST
NNTP-Posting-Host a33b28e4.newsspool4.arcor-online.net
X-Trace DXC=T<L\?X_4gEg_0Po7BmQ3]l4IUK<Cl32<a4Fo<]lROoRa8kF<OcfhCOkOSYjad_SmciDZm8W4\YJNlb@mF9jNikdemA0dJ;NON0oYGBDTLF`D6j
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:21335

Show key headers only | View raw


Daniel Pitts wrote:

> On 9/12/13 5:45 PM, Ken wrote:
>> On 12.09.2013 18:39, Daniel Pitts wrote:
>>> On 9/12/13 2:35 PM, Ken wrote:
>>>> 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.
>>> while and for loops should be identical in performance. They are merely
>>> different syntax for the same operations.
>>
>> Well.. it do depend on browser and implementation (such as JIT) but in
>> general while loops are faster as seen here (with most browsers):
>>
>> http://jsperf.com/fastest-array-loops-in-javascript/32
>>
>> Disclaimer: I didn't make the test.
> 
> So many of those tests are flawed. They don't even perform the same
> operation!

ACK.

> Anyway, for my browser, the fastest one was a test I created using a for
> loop:
> 
> for (var l = arr.length; l--;) {
>    someFn(arr[l]);
> }

I expected this; I have recommended this approach before (where reverse 
order is acceptable).  Those are, after all, the fewest operations.

> The second fastest one was a loop that was already there using a while
> loop:
> 
> var l = arr.length;
> while(l--) {
>    someFn(arr[l]);
> }
> 
> The difference in speed was close to the margin-of-error, so they are
> practically equivalent.

That is because they are compiled to the same bytecode or native code.
In pseudo-bytecode:

-------------------------------------------
line   #  operation    return  operands
-------------------------------------------
   1   0  DECL_VAR        $0   "l"
       1  FETCH_VAR       $1   "arr"
       2  FETCH_OBJ_R     $2   $1, "length"
       3  ASSIGN          $3   $0, $2
   2   4  FETCH_VAR       $4   $0
       5  JMPZ                 $4, ->13
       6  POST_DEC        $6   $0
   3   7  FETCH_VAR       $7   $1
       8  FETCH_VAR       $8   $0
       9  FETCH_OBJ_R     $9   $7, $8
      10  FETCH_VAR       $10  "someFn"
      11  CALL_FUNC       $11  $10, $9
      12  JMP                  ->4
      13  RETURN               0
-------------------------------------------

The “for” statement only differ from the “while” statement in that the 
operations #0 to #6 are produced by the same line.
 
> Testing in Chrome 29.0.1547.65 on OS X 10.8.4

Thanks.

-- 
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