<?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: What&#8217;s the Fastest Way to Code a Loop in JavaScript?</title>
	<atom:link href="http://ajaxian.com/archives/fast-loops-in-js/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com/archives/fast-loops-in-js</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Thu, 17 May 2012 07:43:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Milan1980</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-285973</link>
		<dc:creator>Milan1980</dc:creator>
		<pubDate>Thu, 15 Dec 2011 23:44:49 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-285973</guid>
		<description>I think I just discovered the fastest JavaScript loop - in my benchmarks over 500% improvement cross-browser.  Read all about it at:

http://milan.adamovsky.com/2011/12/fastest-loop-in-javascript.html

The loop construct looks like this:

var counter = 10;

redo:
while (counter--)
{
... your code ...

continue redo;
}

I haven&#039;t tried all the various loop constructs (e.g. for, do, etc) to see which one is the absolute winner - as the main goal was to prove that the use of a label increases the performance dramatically.</description>
		<content:encoded><![CDATA[<p>I think I just discovered the fastest JavaScript loop &#8211; in my benchmarks over 500% improvement cross-browser.  Read all about it at:</p>
<p><a href="http://milan.adamovsky.com/2011/12/fastest-loop-in-javascript.html" rel="nofollow">http://milan.adamovsky.com/2011/12/fastest-loop-in-javascript.html</a></p>
<p>The loop construct looks like this:</p>
<p>var counter = 10;</p>
<p>redo:<br />
while (counter&#8211;)<br />
{<br />
&#8230; your code &#8230;</p>
<p>continue redo;<br />
}</p>
<p>I haven&#8217;t tried all the various loop constructs (e.g. for, do, etc) to see which one is the absolute winner &#8211; as the main goal was to prove that the use of a label increases the performance dramatically.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remedies</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-269223</link>
		<dc:creator>Remedies</dc:creator>
		<pubDate>Wed, 19 Nov 2008 14:49:58 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-269223</guid>
		<description>OK thanks</description>
		<content:encoded><![CDATA[<p>OK thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: waynep</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266255</link>
		<dc:creator>waynep</dc:creator>
		<pubDate>Wed, 30 Jul 2008 19:29:08 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266255</guid>
		<description>for best readability and performance I&#039;ve been going with:
for (var i=0, len=arr.length; i&lt;len; i++) { }	

If I need to loop through 1000 item array, 100 times (which is rare if never) I&#039;ll start using reverse whiles. :)</description>
		<content:encoded><![CDATA[<p>for best readability and performance I&#8217;ve been going with:<br />
for (var i=0, len=arr.length; i&lt;len; i++) { }	</p>
<p>If I need to loop through 1000 item array, 100 times (which is rare if never) I&#8217;ll start using reverse whiles. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Montago</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266189</link>
		<dc:creator>Montago</dc:creator>
		<pubDate>Tue, 29 Jul 2008 10:12:34 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266189</guid>
		<description>Hmm... I gained a lot, using optimal algorithms for my two most extensive JS applications... 

http://www.mdk-photo.com/Vector3_2 (went from 20ball to 50 at the same fps)

http://www.mdk-photo.com/Editor (doing the impossible every day i code on it...)

I&#039;ve read : http://www.devwebpro.com/devwebpro-39-20030514OptimizingJavaScriptforExecutionSpeed.html
and every application since, executes faster now !

Reverse loop is as good as a For(Assignment) imo.</description>
		<content:encoded><![CDATA[<p>Hmm&#8230; I gained a lot, using optimal algorithms for my two most extensive JS applications&#8230; </p>
<p><a href="http://www.mdk-photo.com/Vector3_2" rel="nofollow">http://www.mdk-photo.com/Vector3_2</a> (went from 20ball to 50 at the same fps)</p>
<p><a href="http://www.mdk-photo.com/Editor" rel="nofollow">http://www.mdk-photo.com/Editor</a> (doing the impossible every day i code on it&#8230;)</p>
<p>I&#8217;ve read : <a href="http://www.devwebpro.com/devwebpro-39-20030514OptimizingJavaScriptforExecutionSpeed.html" rel="nofollow">http://www.devwebpro.com/devwebpro-39-20030514OptimizingJavaScriptforExecutionSpeed.html</a><br />
and every application since, executes faster now !</p>
<p>Reverse loop is as good as a For(Assignment) imo.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Menno</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266188</link>
		<dc:creator>Menno</dc:creator>
		<pubDate>Tue, 29 Jul 2008 08:11:19 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266188</guid>
		<description>I partially agree with SchizoDuckie; in my experience it takes a loooong time of optimizing the actual architecture and code before this sort of optimization is worth the effort. Thankfully, the author agrees:
&lt;blockquote cite=&quot;http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html&quot;&gt;
1) 	The best kind of speed optimization is more efficient algorithm design. Before you go nuts trying to squeeze every last ounce of speed out of every statement, take a serious look at the design of your algorithms first. Why? See next point.
2) 	Speed optimization is usually the last step and the last resort while programming. Code that has been altered explicitly for speed concerns is almost always difficult to read, maintain and overhaul. If you plan on sharing your code with others, this is something you should think about seriously.
3) 	For most uses of JavaScript, speed optimization is just not necessary. If you are feeling the urge to speed up your code just for the sake of it, don&#039;t. It&#039;s not worth it. If, however, speed is critical to your project, and you can&#039;t seem to get the necessary speed from your design, then your code is a good candidate for speed optimization.
&lt;/blockquote&gt;</description>
		<content:encoded><![CDATA[<p>I partially agree with SchizoDuckie; in my experience it takes a loooong time of optimizing the actual architecture and code before this sort of optimization is worth the effort. Thankfully, the author agrees:</p>
<blockquote cite="http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html"><p>
1) 	The best kind of speed optimization is more efficient algorithm design. Before you go nuts trying to squeeze every last ounce of speed out of every statement, take a serious look at the design of your algorithms first. Why? See next point.<br />
2) 	Speed optimization is usually the last step and the last resort while programming. Code that has been altered explicitly for speed concerns is almost always difficult to read, maintain and overhaul. If you plan on sharing your code with others, this is something you should think about seriously.<br />
3) 	For most uses of JavaScript, speed optimization is just not necessary. If you are feeling the urge to speed up your code just for the sake of it, don&#8217;t. It&#8217;s not worth it. If, however, speed is critical to your project, and you can&#8217;t seem to get the necessary speed from your design, then your code is a good candidate for speed optimization.
</p></blockquote>
]]></content:encoded>
	</item>
	<item>
		<title>By: V1</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266187</link>
		<dc:creator>V1</dc:creator>
		<pubDate>Tue, 29 Jul 2008 07:31:31 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266187</guid>
		<description>ok... mili sec savings can be good.. BUT nobody actually tested memory usage... What technique requires the less memory from the system..

Now that would be usefull, because applications grow bigger and the memory get more stuffed..</description>
		<content:encoded><![CDATA[<p>ok&#8230; mili sec savings can be good.. BUT nobody actually tested memory usage&#8230; What technique requires the less memory from the system..</p>
<p>Now that would be usefull, because applications grow bigger and the memory get more stuffed..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nosredna</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266185</link>
		<dc:creator>Nosredna</dc:creator>
		<pubDate>Tue, 29 Jul 2008 03:43:10 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266185</guid>
		<description>I&#039;ve always loved Duff&#039;s Device. It works great and it has a great name.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve always loved Duff&#8217;s Device. It works great and it has a great name.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Flesler</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266184</link>
		<dc:creator>Flesler</dc:creator>
		<pubDate>Tue, 29 Jul 2008 03:35:02 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266184</guid>
		<description>I made a benchmark of my own, one of them is about loops.
I wrote a couple of adapted versions of Duff&#039;s device, some based on existing code.
Here you can see the benchmarker: http://benchmarker.flesler.com/
The test is called &#039;loops&#039;.

Cheers</description>
		<content:encoded><![CDATA[<p>I made a benchmark of my own, one of them is about loops.<br />
I wrote a couple of adapted versions of Duff&#8217;s device, some based on existing code.<br />
Here you can see the benchmarker: <a href="http://benchmarker.flesler.com/" rel="nofollow">http://benchmarker.flesler.com/</a><br />
The test is called &#8216;loops&#8217;.</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PaulIrish</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266183</link>
		<dc:creator>PaulIrish</dc:creator>
		<pubDate>Tue, 29 Jul 2008 02:09:48 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266183</guid>
		<description>http://www.devpro.it/examples/loopsbench/</description>
		<content:encoded><![CDATA[<p><a href="http://www.devpro.it/examples/loopsbench/" rel="nofollow">http://www.devpro.it/examples/loopsbench/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LindsaySmith</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266179</link>
		<dc:creator>LindsaySmith</dc:creator>
		<pubDate>Mon, 28 Jul 2008 22:36:53 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266179</guid>
		<description>Whats also interesting in certain situations is that looping through a linked list is faster than looping through an array.

Eg something like
while (node.next) {
    .. process node.item ...
    node = node.next
}

Also, you can make sure that you always use the same kind of loop over arrays by providing functional style mapping functions eg:

Arrays = {
     // implement array loop using downwards while
     loop: function(ary, lambda) {
               var l = ary.length;
               var i, n;
               if (l &gt; 0) {
                    i = l;
                    do {
                        n = l - i;
                        lambda(ary[n], n);
                        i -= 1;
                    } while (i);
               }
     }
}
eg:
Arrays.loop([1,2,3], function(i) { console.log(i) });

This way you can keep the loop code seperate so it can be improved or modified.  You lose the ability to break out of a loop half way through but this can be achieved with return values or exceptions.</description>
		<content:encoded><![CDATA[<p>Whats also interesting in certain situations is that looping through a linked list is faster than looping through an array.</p>
<p>Eg something like<br />
while (node.next) {<br />
    .. process node.item &#8230;<br />
    node = node.next<br />
}</p>
<p>Also, you can make sure that you always use the same kind of loop over arrays by providing functional style mapping functions eg:</p>
<p>Arrays = {<br />
     // implement array loop using downwards while<br />
     loop: function(ary, lambda) {<br />
               var l = ary.length;<br />
               var i, n;<br />
               if (l &gt; 0) {<br />
                    i = l;<br />
                    do {<br />
                        n = l &#8211; i;<br />
                        lambda(ary[n], n);<br />
                        i -= 1;<br />
                    } while (i);<br />
               }<br />
     }<br />
}<br />
eg:<br />
Arrays.loop([1,2,3], function(i) { console.log(i) });</p>
<p>This way you can keep the loop code seperate so it can be improved or modified.  You lose the ability to break out of a loop half way through but this can be achieved with return values or exceptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MattCoz</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266177</link>
		<dc:creator>MattCoz</dc:creator>
		<pubDate>Mon, 28 Jul 2008 22:19:43 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266177</guid>
		<description>@SchizoDuckie: 2004 techniques are completely valid when you need to support a browser from 2001.</description>
		<content:encoded><![CDATA[<p>@SchizoDuckie: 2004 techniques are completely valid when you need to support a browser from 2001.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nosredna</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266172</link>
		<dc:creator>Nosredna</dc:creator>
		<pubDate>Mon, 28 Jul 2008 19:17:13 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266172</guid>
		<description>I use reverse loops a couple places where it&#039;s critical. Otherwise, I don&#039;t bother.</description>
		<content:encoded><![CDATA[<p>I use reverse loops a couple places where it&#8217;s critical. Otherwise, I don&#8217;t bother.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MrMuskrat</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266171</link>
		<dc:creator>MrMuskrat</dc:creator>
		<pubDate>Mon, 28 Jul 2008 18:47:34 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266171</guid>
		<description>@SchizoDuckie: It is *not* micro optimization when you consider how long IE takes to loop through arrays and collections.</description>
		<content:encoded><![CDATA[<p>@SchizoDuckie: It is *not* micro optimization when you consider how long IE takes to loop through arrays and collections.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jpsykes</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266169</link>
		<dc:creator>jpsykes</dc:creator>
		<pubDate>Mon, 28 Jul 2008 17:18:15 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266169</guid>
		<description>I always thought the fastest was the Duff.

http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html

But it&#039;s a total nightmare to implement.

I never really understood why the reverse while didn&#039;t catch on more.  It&#039;s easy to use, doesn&#039;t really need any more code and it much faster than a regular for loop.</description>
		<content:encoded><![CDATA[<p>I always thought the fastest was the Duff.</p>
<p><a href="http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html" rel="nofollow">http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html</a></p>
<p>But it&#8217;s a total nightmare to implement.</p>
<p>I never really understood why the reverse while didn&#8217;t catch on more.  It&#8217;s easy to use, doesn&#8217;t really need any more code and it much faster than a regular for loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jdalton</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266167</link>
		<dc:creator>jdalton</dc:creator>
		<pubDate>Mon, 28 Jul 2008 15:57:55 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266167</guid>
		<description>I heart reverse while loop :P</description>
		<content:encoded><![CDATA[<p>I heart reverse while loop :P</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eyelidlessness</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266166</link>
		<dc:creator>eyelidlessness</dc:creator>
		<pubDate>Mon, 28 Jul 2008 15:47:14 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266166</guid>
		<description>The clearest, most obvious code is almost always the best optimized, as it doesn&#039;t leave as much room to introduce and hide bugs and bottlenecks.</description>
		<content:encoded><![CDATA[<p>The clearest, most obvious code is almost always the best optimized, as it doesn&#8217;t leave as much room to introduce and hide bugs and bottlenecks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SchizoDuckie</title>
		<link>http://ajaxian.com/archives/fast-loops-in-js/comment-page-1#comment-266165</link>
		<dc:creator>SchizoDuckie</dc:creator>
		<pubDate>Mon, 28 Jul 2008 15:29:32 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3938#comment-266165</guid>
		<description>micro optimisations are soooooo 2004</description>
		<content:encoded><![CDATA[<p>micro optimisations are soooooo 2004</p>
]]></content:encoded>
	</item>
</channel>
</rss>

