<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: JavaScript loop performance</title>
	<atom:link href="http://www.robertnyman.com/2008/04/11/javascript-loop-performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/</link>
	<description>Web development and Internet trends</description>
	<pubDate>Thu, 20 Nov 2008 08:12:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-458768</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Sat, 11 Oct 2008 18:14:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-458768</guid>
		<description>Philippe,

Thanks for you input!</description>
		<content:encoded><![CDATA[<p>Philippe,</p>
<p>Thanks for you input!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Philippe Lhoste</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-458338</link>
		<dc:creator>Philippe Lhoste</dc:creator>
		<pubDate>Sat, 11 Oct 2008 08:56:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-458338</guid>
		<description>Interesting.
I am not fan of micro-benchmarks, even less those testing empty loops! You don't know if the compiler (or interpreter) detects them and optimize them.
So I ran a little variation of your code, just putting &lt;code&gt;divs[i] = something&lt;/code&gt; with something changing between each loop (&lt;code&gt;"Jeh" + i&lt;/code&gt; and variants).
Tested on FF3, IE6 and Opera 9.26. It greatly reduces the influence of the loop method, which is logical, but still shows a trend. Actually, it corroborates your finding: &lt;code&gt;divs.length&lt;/code&gt; is still the slowest method! And pre-assigning the length is still the fastest. Note that the for with divs[i] can be optimized as: &lt;code&gt;for (var i=0, d; d=div[i]; i++)&lt;/code&gt;, using d instead of divs[i] in the loop, so the test have an interesting side-effect, and a light speed gain (only one loop up).
Personally, I am not fan of "assign-as-test" but it leads here to results close of your loop3. And unsurprisingly, it is near of the equivalent while loop.
But it is dangerous: as pointed out, if the content of the array can be interpreted as false, it stops the loop! You can still do an explicit &lt;code&gt;(d=div[i])!=null&lt;/code&gt; without time penalty.

So, I will stick with an explicit comparison with a pre-assigned length variable.

Note: in the increment part of the for loop, i++ and ++i has no importance, you can even write i += 1 if you wish (probably slower), the interpreter will just look at (and use) the resulting i.</description>
		<content:encoded><![CDATA[<p>Interesting.<br />
I am not fan of micro-benchmarks, even less those testing empty loops! You don&#8217;t know if the compiler (or interpreter) detects them and optimize them.<br />
So I ran a little variation of your code, just putting <code>divs[i] = something</code> with something changing between each loop (<code>"Jeh" + i</code> and variants).<br />
Tested on FF3, IE6 and Opera 9.26. It greatly reduces the influence of the loop method, which is logical, but still shows a trend. Actually, it corroborates your finding: <code>divs.length</code> is still the slowest method! And pre-assigning the length is still the fastest. Note that the for with divs[i] can be optimized as: <code>for (var i=0, d; d=div[i]; i++)</code>, using d instead of divs[i] in the loop, so the test have an interesting side-effect, and a light speed gain (only one loop up).<br />
Personally, I am not fan of &#8220;assign-as-test&#8221; but it leads here to results close of your loop3. And unsurprisingly, it is near of the equivalent while loop.<br />
But it is dangerous: as pointed out, if the content of the array can be interpreted as false, it stops the loop! You can still do an explicit <code>(d=div[i])!=null</code> without time penalty.</p>
<p>So, I will stick with an explicit comparison with a pre-assigned length variable.</p>
<p>Note: in the increment part of the for loop, i++ and ++i has no importance, you can even write i += 1 if you wish (probably slower), the interpreter will just look at (and use) the resulting i.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392579</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 27 Aug 2008 11:08:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392579</guid>
		<description>Steve,

Yep, IE is &lt;em&gt;so&lt;/em&gt; far behind their competitors, in any way imaginable...</description>
		<content:encoded><![CDATA[<p>Steve,</p>
<p>Yep, <acronym title="Internet Explorer">IE</acronym> is <em>so</em> far behind their competitors, in any way imaginable&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392547</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Wed, 27 Aug 2008 10:26:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392547</guid>
		<description>Wow, thanks for the speedy reply.

I stuck with doing the nextSibling approach in ie and 3rd loop in everything else. The loop 3 seemed to work fine in ie as well. My problem was accessing other DOM elements in each iteration - just had to cache these and it was all good. Although its off the topic of the post, there was never a problem in anything other than ie even before caching (execution &#60; 2 secs) while ie was taking more than 30 secs for the same function. I guess the optimization of the other compilers is just better</description>
		<content:encoded><![CDATA[<p>Wow, thanks for the speedy reply.</p>
<p>I stuck with doing the nextSibling approach in <acronym title="Internet Explorer">IE</acronym> and 3rd loop in everything else. The loop 3 seemed to work fine in <acronym title="Internet Explorer">IE</acronym> as well. My problem was accessing other <acronym title="Document Object Model">DOM</acronym> elements in each iteration - just had to cache these and it was all good. Although its off the topic of the post, there was never a problem in anything other than <acronym title="Internet Explorer">IE</acronym> even before caching (execution &lt; 2 secs) while <acronym title="Internet Explorer">IE</acronym> was taking more than 30 secs for the same function. I guess the optimization of the other compilers is just better</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392519</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 27 Aug 2008 09:58:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392519</guid>
		<description>Luc,

Thanks!

Morris,

I know that variable declarations goes for the entire function and not just the loop itself. But I can't see how this would matter here?

Greg,

Well, they might be, but my guess is that the results would be about the same.

Steve,

Actually, it returns a HTML collection in all web browsers (like a light-version of an array). So, I'm not sure IE works the way you describe it, meaning that it would probably not be necessary to loop through the &lt;code&gt;childNode&lt;/code&gt;s. In my experiences, at least with IE, getting &lt;code&gt;childNode&lt;/code&gt;s has been fairly much slower than, for instance, &lt;code&gt;getElementsByTagName&lt;/code&gt;.

However, I can't give you a definitive answer; I guess it all comes down to testing, testing, testing.</description>
		<content:encoded><![CDATA[<p>Luc,</p>
<p>Thanks!</p>
<p>Morris,</p>
<p>I know that variable declarations goes for the entire function and not just the loop itself. But I can&#8217;t see how this would matter here?</p>
<p>Greg,</p>
<p>Well, they might be, but my guess is that the results would be about the same.</p>
<p>Steve,</p>
<p>Actually, it returns a <acronym title="HyperText Markup Language">HTML</acronym> collection in all web browsers (like a light-version of an array). So, I&#8217;m not sure <acronym title="Internet Explorer">IE</acronym> works the way you describe it, meaning that it would probably not be necessary to loop through the <code>childNode</code>s. In my experiences, at least with <acronym title="Internet Explorer">IE</acronym>, getting <code>childNode</code>s has been fairly much slower than, for instance, <code>getElementsByTagName</code>.</p>
<p>However, I can&#8217;t give you a definitive answer; I guess it all comes down to testing, testing, testing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392429</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Wed, 27 Aug 2008 07:59:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-392429</guid>
		<description>Hi,

I wondered wether it has to do with the way that collections are  implemented in different version of javascript. The dom method getElementsByTagName returns an array in firefox etc. but in IE it returns a linked list. When you are setting up your loop as 

index=0; index &#60; divs.length; index++ 

ie needs to loop through the whole list to calculate the length for each iteration giving you O(n2). Please could someone confirm this for me. 

My other question is that if this is in fact true, when you index the div in the 'array' as divs[index] does ie need to loop through the linked list to find the correct position giving you O(n2) again? perhaps its better to get the parent element and then loop through the child nodes using while (currentNode.nextSibling) { 
//do stuff
currentNode = currentNode.nextSibling; }

Any info would be greatly appreciated because I've having some trouble with this for a while</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I wondered wether it has to do with the way that collections are  implemented in different version of javascript. The <acronym title="Document Object Model">DOM</acronym> method getElementsByTagName returns an array in firefox etc. but in <acronym title="Internet Explorer">IE</acronym> it returns a linked list. When you are setting up your loop as </p>
<p>index=0; index &lt; divs.length; index++ </p>
<p><acronym title="Internet Explorer">IE</acronym> needs to loop through the whole list to calculate the length for each iteration giving you O(n2). Please could someone confirm this for me. </p>
<p>My other question is that if this is in fact true, when you index the div in the &#8216;array&#8217; as divs[index] does <acronym title="Internet Explorer">IE</acronym> need to loop through the linked list to find the correct position giving you O(n2) again? perhaps its better to get the parent element and then loop through the child nodes using while (currentNode.nextSibling) {<br />
//do stuff<br />
currentNode = currentNode.nextSibling; }</p>
<p>Any info would be greatly appreciated because I&#8217;ve having some trouble with this for a while</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-348877</link>
		<dc:creator>Greg</dc:creator>
		<pubDate>Tue, 22 Jul 2008 19:15:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-348877</guid>
		<description>Another observation, loop 2 will fail on sparse arrays.</description>
		<content:encoded><![CDATA[<p>Another observation, loop 2 will fail on sparse arrays.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-348876</link>
		<dc:creator>Greg</dc:creator>
		<pubDate>Tue, 22 Jul 2008 19:14:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-348876</guid>
		<description>I notice that you're looping through an HTML collection, i.e. the result of getElementsByTagName(). My understanding is that this is not a true native JavaScript array. I wonder if the results would be different if you transferred the contents of the HTML collection into a native array prior to doing the benchmarks.</description>
		<content:encoded><![CDATA[<p>I notice that you&#8217;re looping through an <acronym title="HyperText Markup Language">HTML</acronym> collection, i.e. the result of getElementsByTagName(). My understanding is that this is not a true native JavaScript array. I wonder if the results would be different if you transferred the contents of the <acronym title="HyperText Markup Language">HTML</acronym> collection into a native array prior to doing the benchmarks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Morris</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-340895</link>
		<dc:creator>Morris</dc:creator>
		<pubDate>Wed, 16 Jul 2008 03:43:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-340895</guid>
		<description>Robert, Noevov, Gerben: how variable declarations work in Javascript is commonly misunderstood &#8212; the following do not do what you might expect:
&lt;code&gt;
function test1() {
	for (var i=1; i&#60;2; i++) {
	}
	alert('test 1: i=' + String(i));
}
function test2() {
	for (i=1; i&#60;2; i++) {
	}
	j=10;
	alert('test 2:  i=' + String(i) + ' window.i=' + String(window.i) + ' window.j=' + String(window.j));
	var i;
}
test1();
test2();
&lt;/code&gt;
The rule is that a var declaration anywhere in a function applies everywhere within a function. This means:


Making the var declaration in the for loop doesn't mean the variable is scoped to that loop - it is scoped to the function (as per test1 - which alerts i=2).


A local variable can be declared after it is actually used (as per test2 - which also alerts i=2).

</description>
		<content:encoded><![CDATA[<p>Robert, Noevov, Gerben: how variable declarations work in Javascript is commonly misunderstood &mdash; the following do not do what you might expect:<br />
<code><br />
function test1() {<br />
	for (var i=1; i&lt;2; i++) {<br />
	}<br />
	alert('test 1: i=' + String(i));<br />
}<br />
function test2() {<br />
	for (i=1; i&lt;2; i++) {<br />
	}<br />
	j=10;<br />
	alert('test 2:  i=' + String(i) + ' window.i=' + String(window.i) + ' window.j=' + String(window.j));<br />
	var i;<br />
}<br />
test1();<br />
test2();<br />
</code><br />
The rule is that a var declaration anywhere in a function applies everywhere within a function. This means:</p>
<p>Making the var declaration in the for loop doesn&#8217;t mean the variable is scoped to that loop - it is scoped to the function (as per test1 - which alerts i=2).</p>
<p>A local variable can be declared after it is actually used (as per test2 - which also alerts i=2).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luc</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-334571</link>
		<dc:creator>Luc</dc:creator>
		<pubDate>Thu, 10 Jul 2008 10:37:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-334571</guid>
		<description>Here are some additional results for you.  The Array.forEach is build into .NET javascript.  My timings are over 2,000 elements and I am doing the following statement in each for loop:

div.innerHTML = 'A';

1)    Loop: for (var i=0; i&#60;divs.length; i++)
437

2)    for (var i=0; divs[i]; i++)
407

3)    for (var i=0, il=divs.length; i&#60;il; i++)
406

4)    for (var div in divs)
422

5)    Array.forEach(divs, function)
422

SO THE WINNER IS (3) which is roberts pick.  It would use slightly more more memory though (barely).</description>
		<content:encoded><![CDATA[<p>Here are some additional results for you.  The Array.forEach is build into .NET javascript.  My timings are over 2,000 elements and I am doing the following statement in each for loop:</p>
<p>div.innerHTML = &#8216;A&#8217;;</p>
<p>1)    Loop: for (var i=0; i&lt;divs.length; i++)<br />
437</p>
<p>2)    for (var i=0; divs[i]; i++)<br />
407</p>
<p>3)    for (var i=0, il=divs.length; i&lt;il; i++)<br />
406</p>
<p>4)    for (var div in divs)<br />
422</p>
<p>5)    Array.forEach(divs, function)<br />
422</p>
<p>SO THE WINNER IS (3) which is roberts pick.  It would use slightly more more memory though (barely).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-261324</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 30 Apr 2008 11:45:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-261324</guid>
		<description>Christian,

Thanks for the tip! I haven't read that post of yours, actually, but it was very very similar. :-)

The benchmarking is extremely basic, so I agree that this should just be a base for more extensive testing to be done.</description>
		<content:encoded><![CDATA[<p>Christian,</p>
<p>Thanks for the tip! I haven&#8217;t read that post of yours, actually, but it was very very similar. <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The benchmarking is extremely basic, so I agree that this should just be a base for more extensive testing to be done.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Heilmann</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-261264</link>
		<dc:creator>Christian Heilmann</dc:creator>
		<pubDate>Wed, 30 Apr 2008 10:22:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-261264</guid>
		<description>I wrote a strinkingly similar &lt;a href="http://www.wait-till-i.com/2007/04/02/the-only-for-loop-you-will-ever-need/" rel="nofollow"&gt;post about different loops and their impace on performance and ease-of-coding&lt;/a&gt; last April, but found that &lt;a href="http://icant.co.uk/sandbox/forloops.html" rel="nofollow"&gt;benchmarks done this way&lt;/a&gt; are pretty useless.

I got a lot of comments though, and there was some really good information in there.</description>
		<content:encoded><![CDATA[<p>I wrote a strinkingly similar <a href="http://www.wait-till-i.com/2007/04/02/the-only-for-loop-you-will-ever-need/" rel="nofollow">post about different loops and their impace on performance and ease-of-coding</a> last April, but found that <a href="http://icant.co.uk/sandbox/forloops.html" rel="nofollow">benchmarks done this way</a> are pretty useless.</p>
<p>I got a lot of comments though, and there was some really good information in there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-259858</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 28 Apr 2008 20:25:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-259858</guid>
		<description>Ricardo,

Interesting! Thanks for sharing.</description>
		<content:encoded><![CDATA[<p>Ricardo,</p>
<p>Interesting! Thanks for sharing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ricardo</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-259843</link>
		<dc:creator>Ricardo</dc:creator>
		<pubDate>Mon, 28 Apr 2008 19:33:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-259843</guid>
		<description>Hi, 

In an experiment I'm working on I'm generating 2400 divs and setting it's class with a loop. There is negligible difference between these 3 techniques you showed, loop 2 lags around 100ms behind the others (total time &lt;strong&gt;1.2s&lt;/strong&gt;). I usually set a global var for length if it's used across many functions and the array won't change.

The thing is, using a &lt;i&gt;while&lt;/i&gt; loop instead of &lt;i&gt;for&lt;/i&gt;, the total time comes down to &lt;strong&gt;0.8s&lt;/strong&gt;. Also tried loop unrolling/duff's device with no effect, so I'm staying with &lt;i&gt;while&lt;/i&gt; for now :]</description>
		<content:encoded><![CDATA[<p>Hi, </p>
<p>In an experiment I&#8217;m working on I&#8217;m generating 2400 divs and setting it&#8217;s class with a loop. There is negligible difference between these 3 techniques you showed, loop 2 lags around 100ms behind the others (total time <strong>1.2s</strong>). I usually set a global var for length if it&#8217;s used across many functions and the array won&#8217;t change.</p>
<p>The thing is, using a <i>while</i> loop instead of <i>for</i>, the total time comes down to <strong>0.8s</strong>. Also tried loop unrolling/duff&#8217;s device with no effect, so I&#8217;m staying with <i>while</i> for now :]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252291</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 18 Apr 2008 15:16:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252291</guid>
		<description>Fredrik, liorean,

Those are two interesting approaches. I haven't tried them out yet, but in theory they look interesting! :-)

When it comes to timers and actually doing something within the loop, I'm well aware that it would change the results, but at the same time, then the timers can be thrown off by some irregularity in what's done within them.

So, please see my tests as a base for doing more and more extensive tests.</description>
		<content:encoded><![CDATA[<p>Fredrik, liorean,</p>
<p>Those are two interesting approaches. I haven&#8217;t tried them out yet, but in theory they look interesting! <img src='http://www.robertnyman.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>When it comes to timers and actually doing something within the loop, I&#8217;m well aware that it would change the results, but at the same time, then the timers can be thrown off by some irregularity in what&#8217;s done within them.</p>
<p>So, please see my tests as a base for doing more and more extensive tests.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: liorean</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252197</link>
		<dc:creator>liorean</dc:creator>
		<pubDate>Fri, 18 Apr 2008 12:53:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252197</guid>
		<description>Oh, and your post preview doesn't quite match the real posted comment, I see...</description>
		<content:encoded><![CDATA[<p>Oh, and your post preview doesn&#8217;t quite match the real posted comment, I see&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: liorean</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252196</link>
		<dc:creator>liorean</dc:creator>
		<pubDate>Fri, 18 Apr 2008 12:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252196</guid>
		<description>&lt;b&gt;Robert&lt;/b&gt;: There's at least one other loop construct I would recommend:&lt;code&gt;   while(elm=coll.item(i++))&lt;/code&gt;In this syntax you use the &lt;code&gt;elm&lt;/code&gt; variable inside the loop body instead of using &lt;code&gt;divs[i]&lt;/code&gt; as your last example would require. (You can of course consider this a variation of second loop, which I'd write as:&lt;code&gt;    for(i=0;elm=divs.item(i++);&#38;#x29&lt;/code&gt;Why do I use &lt;code&gt;.item&lt;/code&gt;? IIRC there are cases where &lt;code&gt;coll[i]&lt;/code&gt; throws an exception but &lt;code&gt;coll.item(i)&lt;/code&gt; instead returns &lt;code&gt;null&lt;/code&gt;. It's browser dependent though - some browsers actually return &lt;code&gt;null&lt;/code&gt; for the hash access too.Also, I'd want to see you do those timings with an explicit lookup in each of the loop bodies, because in real live code you're not going to loop through these collections without actually using the elements for anything, and basing your timing of each example where you're actually not doing the most expensive part of the loop invariant which is always there in real world situations scews the results.</description>
		<content:encoded><![CDATA[<p><b>Robert</b>: There&#8217;s at least one other loop construct I would recommend:<code>   while(elm=coll.item(i++))</code>In this syntax you use the <code>elm</code> variable inside the loop body instead of using <code>divs[i]</code> as your last example would require. (You can of course consider this a variation of second loop, which I&#8217;d write as:<code>    for(i=0;elm=divs.item(i++);&amp;#x29</code>Why do I use <code>.item</code>? <acronym title="if I remember correctly">IIRC</acronym> there are cases where <code>coll[i]</code> throws an exception but <code>coll.item(i)</code> instead returns <code>null</code>. It&#8217;s browser dependent though - some browsers actually return <code>null</code> for the hash access too.Also, I&#8217;d want to see you do those timings with an explicit lookup in each of the loop bodies, because in real live code you&#8217;re not going to loop through these collections without actually using the elements for anything, and basing your timing of each example where you&#8217;re actually not doing the most expensive part of the loop invariant which is always there in real world situations scews the results.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fredrik</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252155</link>
		<dc:creator>Fredrik</dc:creator>
		<pubDate>Fri, 18 Apr 2008 11:43:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-252155</guid>
		<description>Have you tried profiling the prefix ++ notation? 
i.e: &lt;code&gt;for (var i = 0; i &#60; len; ++i)&lt;/code&gt;
in theory it should be slightly faster since it only does exactly what you want, incrementing the var, Not incrementing and then returning the previous value..</description>
		<content:encoded><![CDATA[<p>Have you tried profiling the prefix ++ notation?<br />
i.e: <code>for (var i = 0; i &lt; len; ++i)</code><br />
in theory it should be slightly faster since it only does exactly what you want, incrementing the var, Not incrementing and then returning the previous value..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript loop performance &#124; gosnip.net blog</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-251370</link>
		<dc:creator>JavaScript loop performance &#124; gosnip.net blog</dc:creator>
		<pubDate>Thu, 17 Apr 2008 12:21:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-251370</guid>
		<description>[...] Nyman, the original author of the perfect little JS library, DOMAssistant, has done a little loop speed test and has come to a very useful conclusion that saves a lot of processing time. Next time you do a [...]</description>
		<content:encoded><![CDATA[<p>[...] Nyman, the original author of the perfect little JS library, DOMAssistant, has done a little loop speed test and has come to a very useful conclusion that saves a lot of processing time. Next time you do a [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2008-04-15 &#124; Libin Pan</title>
		<link>http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-249762</link>
		<dc:creator>links for 2008-04-15 &#124; Libin Pan</dc:creator>
		<pubDate>Tue, 15 Apr 2008 06:46:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-249762</guid>
		<description>[...] JavaScript loop performance - Robert’s talk - Web development and Internet trends JavaScript loop performance (tags: ajax javascript performance) [...]</description>
		<content:encoded><![CDATA[<p>[...] JavaScript loop performance - Robert’s talk - Web development and Internet trends JavaScript loop performance (tags: <acronym title="Asynchronous Javascript and XML">AJAX</acronym> javascript performance) [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
