<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: JavaScript loop performance</title>
	<atom:link href="http://robertnyman.com/2008/04/11/javascript-loop-performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/</link>
	<description>Web development and Internet trends</description>
	<lastBuildDate>Fri, 19 Mar 2010 11:26:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625018</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Tue, 02 Mar 2010 16:48:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625018</guid>
		<description>Deepak,

Tag names or possibly class names.</description>
		<content:encoded><![CDATA[<p>Deepak,</p>
<p>Tag names or possibly class names.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625015</link>
		<dc:creator>Deepak</dc:creator>
		<pubDate>Tue, 02 Mar 2010 16:39:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625015</guid>
		<description>Any ideas on the alternate way.

I tried with TagNames, but not working as expected</description>
		<content:encoded><![CDATA[<p>Any ideas on the alternate way.</p>
<p>I tried with TagNames, but not working as expected</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625014</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Tue, 02 Mar 2010 16:24:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625014</guid>
		<description>Deepak,

IDs are unique, i.e. only one element can have the same ID. Try to approach it some other way then.</description>
		<content:encoded><![CDATA[<p>Deepak,</p>
<p>IDs are unique, i.e. only one element can have the same ID. Try to approach it some other way then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625007</link>
		<dc:creator>Deepak</dc:creator>
		<pubDate>Tue, 02 Mar 2010 15:44:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-625007</guid>
		<description>I tried using the document.getElementById by assigning one ID for many elements. 
When I run the below line
document.getElementById(&#039;trial&#039;).innerHTML = &#039;&#039;;

only the first occurance is replaced, but the subsequent occurances are not replaced. Any pointers on this is highly appreciated</description>
		<content:encoded><![CDATA[<p>I tried using the document.getElementById by assigning one ID for many elements.<br />
When I run the below line<br />
document.getElementById(&#8216;trial&#8217;).innerHTML = &#8221;;</p>
<p>only the first occurance is replaced, but the subsequent occurances are not replaced. Any pointers on this is highly appreciated</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622075</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 12 Feb 2010 21:47:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622075</guid>
		<description>Deepak,

It&#039;s just a matter of having a variable and appending the string content for each loop iteration. You need to reference the element with document.getElementById(&quot;element-id&quot;).innerHTML = yourStringVariableWithHTML</description>
		<content:encoded><![CDATA[<p>Deepak,</p>
<p>It&#8217;s just a matter of having a variable and appending the string content for each loop iteration. You need to reference the element with document.getElementById(&#8220;element-id&#8221;).innerHTML = yourStringVariableWithHTML</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622073</link>
		<dc:creator>Deepak</dc:creator>
		<pubDate>Fri, 12 Feb 2010 21:33:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622073</guid>
		<description>Thanks for the reply

This is a report which pops out in the preview mode. So, in this case how do I write using .innherHTML.

Also, I&#039;m storing all the values in the array, so without the for loop am not sure how to put them to the report. 

Any pointers would be really appreciated

Deepak</description>
		<content:encoded><![CDATA[<p>Thanks for the reply</p>
<p>This is a report which pops out in the preview mode. So, in this case how do I write using .innherHTML.</p>
<p>Also, I&#8217;m storing all the values in the array, so without the for loop am not sure how to put them to the report. </p>
<p>Any pointers would be really appreciated</p>
<p>Deepak</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622065</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 12 Feb 2010 20:02:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622065</guid>
		<description>Deepak,

I&#039;m sure the problem lies in document.write, where it for each instance has to write to the document. Try adding the values to a variable, and then, after the loop, write to the document (and rather by using .innerHTML on the element where you want to insert content).

Also, that many lines with such a huge number is destined to become slow in a web browser. 

Good luck!</description>
		<content:encoded><![CDATA[<p>Deepak,</p>
<p>I&#8217;m sure the problem lies in document.write, where it for each instance has to write to the document. Try adding the values to a variable, and then, after the loop, write to the document (and rather by using .innerHTML on the element where you want to insert content).</p>
<p>Also, that many lines with such a huge number is destined to become slow in a web browser. </p>
<p>Good luck!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622063</link>
		<dc:creator>Deepak</dc:creator>
		<pubDate>Fri, 12 Feb 2010 19:51:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622063</guid>
		<description>Sorry forgot to add one more thing in the above post.

I have used all the different types of looping suggested by Robert, but the results have not changed.</description>
		<content:encoded><![CDATA[<p>Sorry forgot to add one more thing in the above post.</p>
<p>I have used all the different types of looping suggested by Robert, but the results have not changed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622062</link>
		<dc:creator>Deepak</dc:creator>
		<pubDate>Fri, 12 Feb 2010 19:49:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-622062</guid>
		<description>One of my javascript code is running very slow, maybe somebody can help me with it.

The Code is 

***************************************
temp = (totalexists==1)? bookinggridcols+1 : bookinggridcols-1;
	for (i=0;i&lt;=temp;i++) 
			document.write(&quot;&quot; + bookinggridvalues[i] + &quot;&quot;); 
****************************************

This spits out a huge table of 3,654 rows with each row containing 35 cells.

I find that the above code, initially starts spitting out each row in 0.015 secs but as the file starts to grow, eventually it takes 1.5 secs for each row (after it has done around 1000 rows), and magically the last 600 odd rows becomes faster at around less 0.2 secs for each row.

The time I have quoted is for actually printing each row, and there is no calculation involved as the values are stored in an array. The array size is always maintained at 35 in this example, but is free to grow if required. After each for loop, there is other code which populates the array, and that time is not included in the above times I have mentioned. 

Any help in this regard will be hugely appreciated.</description>
		<content:encoded><![CDATA[<p>One of my javascript code is running very slow, maybe somebody can help me with it.</p>
<p>The Code is </p>
<p>***************************************<br />
temp = (totalexists==1)? bookinggridcols+1 : bookinggridcols-1;<br />
	for (i=0;i&lt;=temp;i++)<br />
			document.write(&quot;&#8221; + bookinggridvalues[i] + &#8220;&#8221;);<br />
****************************************</p>
<p>This spits out a huge table of 3,654 rows with each row containing 35 cells.</p>
<p>I find that the above code, initially starts spitting out each row in 0.015 secs but as the file starts to grow, eventually it takes 1.5 secs for each row (after it has done around 1000 rows), and magically the last 600 odd rows becomes faster at around less 0.2 secs for each row.</p>
<p>The time I have quoted is for actually printing each row, and there is no calculation involved as the values are stored in an array. The array size is always maintained at 35 in this example, but is free to grow if required. After each for loop, there is other code which populates the array, and that time is not included in the above times I have mentioned. </p>
<p>Any help in this regard will be hugely appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-586351</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Fri, 29 May 2009 14:56:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-586351</guid>
		<description>After reviewing everyone&#039;s post and testing out the methods.  I have come to the conclusion that the best way to do it is through example 3 that Robert posted or reversing it to check zero and count down.  You can also do a while or do while loop but the speed difference between them are neglible and &lt;i&gt;for&lt;/i&gt; loops are easier to read usually.  

I personally have decided to switch to using example three except reversed and counting down to zero.  I am not sure if there is any speed difference, but if there is then I need to boost as much performance as I can out of the loop for my application of it.</description>
		<content:encoded><![CDATA[<p>After reviewing everyone&#8217;s post and testing out the methods.  I have come to the conclusion that the best way to do it is through example 3 that Robert posted or reversing it to check zero and count down.  You can also do a while or do while loop but the speed difference between them are neglible and <i>for</i> loops are easier to read usually.  </p>
<p>I personally have decided to switch to using example three except reversed and counting down to zero.  I am not sure if there is any speed difference, but if there is then I need to boost as much performance as I can out of the loop for my application of it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-583688</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 18 May 2009 04:00:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-583688</guid>
		<description>Charles,

I have no concrete example here, but from what I know it&#039;s not faster. Another thing to generally think about with &lt;code&gt;for (var i in)&lt;/code&gt; loops is that it goes through all data types and members in that collection, often resulting in unexpected behavior and/or values.

Therefore, you would need to add a check to each iteration (for instance, to make sure it&#039;s really a string you&#039;re trying to manipulate), and that would definitely hurt the performance.</description>
		<content:encoded><![CDATA[<p>Charles,</p>
<p>I have no concrete example here, but from what I know it&#8217;s not faster. Another thing to generally think about with <code>for (var i in)</code> loops is that it goes through all data types and members in that collection, often resulting in unexpected behavior and/or values.</p>
<p>Therefore, you would need to add a check to each iteration (for instance, to make sure it&#8217;s really a string you&#8217;re trying to manipulate), and that would definitely hurt the performance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-583209</link>
		<dc:creator>Charles</dc:creator>
		<pubDate>Fri, 15 May 2009 16:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-583209</guid>
		<description>What about:

for (var i in items) {
  // Do something.
}</description>
		<content:encoded><![CDATA[<p>What about:</p>
<p>for (var i in items) {<br />
  // Do something.<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-563180</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 06 Apr 2009 11:09:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-563180</guid>
		<description>Tim,

Generally, looping backwards is very fast, but not always suitable in all scenarios.</description>
		<content:encoded><![CDATA[<p>Tim,</p>
<p>Generally, looping backwards is very fast, but not always suitable in all scenarios.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-563175</link>
		<dc:creator>Tim</dc:creator>
		<pubDate>Mon, 06 Apr 2009 11:03:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-563175</guid>
		<description>How would:
&lt;code&gt;for(var i=array.length-1;i&gt;=0;i--) {
    // Magic
}&lt;/code&gt;
compare to loop 3?</description>
		<content:encoded><![CDATA[<p>How would:<br />
<code>for(var i=array.length-1;i&gt;=0;i--) {<br />
    // Magic<br />
}</code><br />
compare to loop 3?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-547341</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Wed, 18 Mar 2009 20:32:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-547341</guid>
		<description>Niels,

Absolutely, that can be a good option.

Suresh,

Glad to hear that it helped!</description>
		<content:encoded><![CDATA[<p>Niels,</p>
<p>Absolutely, that can be a good option.</p>
<p>Suresh,</p>
<p>Glad to hear that it helped!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Suresh</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-547332</link>
		<dc:creator>Suresh</dc:creator>
		<pubDate>Wed, 18 Mar 2009 20:17:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-547332</guid>
		<description>Thanks So much. I was confused why my page took 75 seconds in IE and 5 seconds in Firefox. Now i fixed the issue. Thanks a ton.

Thanks
Baba</description>
		<content:encoded><![CDATA[<p>Thanks So much. I was confused why my page took 75 seconds in IE and 5 seconds in Firefox. Now i fixed the issue. Thanks a ton.</p>
<p>Thanks<br />
Baba</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Gawron: Wydajno?? i optymalizacja zapyta? do bazy danych</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-514417</link>
		<dc:creator>Robert Gawron: Wydajno?? i optymalizacja zapyta? do bazy danych</dc:creator>
		<pubDate>Wed, 04 Feb 2009 21:26:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-514417</guid>
		<description>[...] Co? podobnego, tylko ?e dla JavaScript &#8212; fajna lektura, chocia? problem, dla ktÃ³rego autor bada? wydajno?? nie jest zbyt&#160;ciekawy/wa?ny.                                    Comment Comments No comments for this post [...]</description>
		<content:encoded><![CDATA[<p>[...] Co? podobnego, tylko ?e dla JavaScript &mdash; fajna lektura, chocia? problem, dla ktÃ³rego autor bada? wydajno?? nie jest zbyt&nbsp;ciekawy/wa?ny.                                    Comment Comments No comments for this post [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niels Steenbeek</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-514190</link>
		<dc:creator>Niels Steenbeek</dc:creator>
		<pubDate>Wed, 04 Feb 2009 15:45:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-514190</guid>
		<description>Maybe I may suggest loop 4
It loops from end to start, but is the quickest one
for(var i = divs.length - 1; i &gt;= 0; --i){
	// Magic
}</description>
		<content:encoded><![CDATA[<p>Maybe I may suggest loop 4<br />
It loops from end to start, but is the quickest one<br />
for(var i = divs.length &#8211; 1; i &gt;= 0; &#8211;i){<br />
	// Magic<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JavaScript?????? &#124; ????</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-504469</link>
		<dc:creator>JavaScript?????? &#124; ????</dc:creator>
		<pubDate>Thu, 22 Jan 2009 01:01:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-504469</guid>
		<description>[...] ?????????????????????????????????JavaScript??????????? JavaScript loop performance [...]</description>
		<content:encoded><![CDATA[<p>[...] ?????????????????????????????????JavaScript??????????? JavaScript loop performance [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://robertnyman.com/2008/04/11/javascript-loop-performance/#comment-495012</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Mon, 05 Jan 2009 00:38:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.robertnyman.com/2008/04/11/javascript-loop-performance/#comment-495012</guid>
		<description>Jhuni,

Thanks for your comment!
While I don&#039;t necessarily agree about readability, you have some thought-worthy points.</description>
		<content:encoded><![CDATA[<p>Jhuni,</p>
<p>Thanks for your comment!<br />
While I don&#8217;t necessarily agree about readability, you have some thought-worthy points.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
