<?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: How many ways can you iterate over an array in JavaScript?</title>
	<atom:link href="http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Thu, 09 Feb 2012 06:55:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: molokoloco</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273105</link>
		<dc:creator>molokoloco</dc:creator>
		<pubDate>Mon, 27 Apr 2009 00:35:31 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273105</guid>
		<description>Like some of you I heard this is the right way
var browsers = new Array(&#039;opera&#039;,&#039;safari&#039;,&#039;firefox&#039;,&#039;...&#039;);
for (var index = 0, len = browsers.length; index &lt; len; ++index) alert(browsers[index]);

But know i see that it seem most logical, no ?
browsers.forEach(function(item) { alert(item); });</description>
		<content:encoded><![CDATA[<p>Like some of you I heard this is the right way<br />
var browsers = new Array(&#8216;opera&#8217;,'safari&#8217;,'firefox&#8217;,'&#8230;&#8217;);<br />
for (var index = 0, len = browsers.length; index &lt; len; ++index) alert(browsers[index]);</p>
<p>But know i see that it seem most logical, no ?<br />
browsers.forEach(function(item) { alert(item); });</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: patspam</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273088</link>
		<dc:creator>patspam</dc:creator>
		<pubDate>Sat, 25 Apr 2009 04:12:45 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273088</guid>
		<description>@duncanbeevers: Javascript doesn&#039;t have block scope. Those aren&#039;t &quot;loop-local&quot; variables, which is why using c-style loops kinda sucks in Javascript.

For instance, guess what this displays:
var i = 50;
for( var i = 0; i&lt; 10; i++ ) {}
alert(i);

Every time you use a for loop and define a loop variable, that variable is scoped to the containing function, not the loop block.

I miss perl-style map and foreach every time I write a loop in Javascript.

Patrick</description>
		<content:encoded><![CDATA[<p>@duncanbeevers: Javascript doesn&#8217;t have block scope. Those aren&#8217;t &#8220;loop-local&#8221; variables, which is why using c-style loops kinda sucks in Javascript.</p>
<p>For instance, guess what this displays:<br />
var i = 50;<br />
for( var i = 0; i&lt; 10; i++ ) {}<br />
alert(i);</p>
<p>Every time you use a for loop and define a loop variable, that variable is scoped to the containing function, not the loop block.</p>
<p>I miss perl-style map and foreach every time I write a loop in Javascript.</p>
<p>Patrick</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: duncanbeevers</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273070</link>
		<dc:creator>duncanbeevers</dc:creator>
		<pubDate>Fri, 24 Apr 2009 14:00:02 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273070</guid>
		<description>JavaScript&#039;s for-loop construct allows you to declare multiple loop-local variables, which is useful for memoizing array length without leaking variables into the outer scope.

&lt;code&gt;
var a = [ 1, 2, 3 ];
for (var i = 0, len = a.length; i &lt; len; i++) {
  // work
}
&lt;/code&gt;

Also, though I haven&#039;t benchmarked it, when order of traversal isn&#039;t important, I typically prefer fewer locals and opt instead of constant comparison.

&lt;code&gt;
for (var i = a.length - 1; i &gt;= 0; i--) {
  // work
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>JavaScript&#8217;s for-loop construct allows you to declare multiple loop-local variables, which is useful for memoizing array length without leaking variables into the outer scope.</p>
<p><code><br />
var a = [ 1, 2, 3 ];<br />
for (var i = 0, len = a.length; i &lt; len; i++) {<br />
  // work<br />
}<br />
</code></p>
<p>Also, though I haven&#8217;t benchmarked it, when order of traversal isn&#8217;t important, I typically prefer fewer locals and opt instead of constant comparison.</p>
<p><code><br />
for (var i = a.length - 1; i &gt;= 0; i--) {<br />
  // work<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aimos</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273066</link>
		<dc:creator>Aimos</dc:creator>
		<pubDate>Fri, 24 Apr 2009 05:58:35 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273066</guid>
		<description>If the .length is done the same way as in java, then it is no optimisation to copy it first in another variable. its just a public attribute of the array (object). If you use things like .length() or .size() then you should do that, because these are method calls and eat time.

Correct me if I am wrong.</description>
		<content:encoded><![CDATA[<p>If the .length is done the same way as in java, then it is no optimisation to copy it first in another variable. its just a public attribute of the array (object). If you use things like .length() or .size() then you should do that, because these are method calls and eat time.</p>
<p>Correct me if I am wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cnizz</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273061</link>
		<dc:creator>cnizz</dc:creator>
		<pubDate>Thu, 23 Apr 2009 20:18:21 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273061</guid>
		<description>I typically use the for loop

var length = arr.length
for(var i=0;i&lt;length;i++){
// code here
}

Based off olliej&#039;s test, its one of the faster ones.  I was certainly surprised to see that recalculating the arr length through each iteration was so costly, but it makes sense in that it adds at best a constant or at worst a linear operation to an already linear process.

You really only need to worry about loop performance if you are dealing with large arrays though, which most javascript applications I develop don&#039;t use.</description>
		<content:encoded><![CDATA[<p>I typically use the for loop</p>
<p>var length = arr.length<br />
for(var i=0;i&lt;length;i++){<br />
// code here<br />
}</p>
<p>Based off olliej&#8217;s test, its one of the faster ones.  I was certainly surprised to see that recalculating the arr length through each iteration was so costly, but it makes sense in that it adds at best a constant or at worst a linear operation to an already linear process.</p>
<p>You really only need to worry about loop performance if you are dealing with large arrays though, which most javascript applications I develop don&#8217;t use.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: olliej</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273059</link>
		<dc:creator>olliej</dc:creator>
		<pubDate>Thu, 23 Apr 2009 19:46:53 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273059</guid>
		<description>@gmariani: added those to the test now.  I had deliberately chosen not to cache the length property, although i honestly cannot recall why :D

@RyanMorr: Actually Array.prototype.forEach (and most other array prototype functions) are defined as being generic, the mozilla MDC documentation gives JS implementations as well and there&#039;s no type checking, basically they&#039;re all

var l = this.length;
for(i = 0;i i&lt;l; i++) { ... }</description>
		<content:encoded><![CDATA[<p>@gmariani: added those to the test now.  I had deliberately chosen not to cache the length property, although i honestly cannot recall why :D</p>
<p>@RyanMorr: Actually Array.prototype.forEach (and most other array prototype functions) are defined as being generic, the mozilla MDC documentation gives JS implementations as well and there&#8217;s no type checking, basically they&#8217;re all</p>
<p>var l = this.length;<br />
for(i = 0;i i&lt;l; i++) { &#8230; }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RyanMorr</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273056</link>
		<dc:creator>RyanMorr</dc:creator>
		<pubDate>Thu, 23 Apr 2009 15:40:13 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273056</guid>
		<description>What about Array.forEach? No not Array.prototype.forEach, it differs because it can handle array-like objects such as nodelists and arguments 

Array.forEach(arguments,  function(){
        //do something
}, scope)

I find it the most useful method for array iterations.</description>
		<content:encoded><![CDATA[<p>What about Array.forEach? No not Array.prototype.forEach, it differs because it can handle array-like objects such as nodelists and arguments </p>
<p>Array.forEach(arguments,  function(){<br />
        //do something<br />
}, scope)</p>
<p>I find it the most useful method for array iterations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gmariani</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273055</link>
		<dc:creator>gmariani</dc:creator>
		<pubDate>Thu, 23 Apr 2009 14:27:17 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273055</guid>
		<description>@olliej

Here are a few more that are a lot quicker
&lt;code&gt;
nopIteration0 : 22ms
function nopIteration0(a, f) {
    var start = new Date;
	var i = a.length;
	while(i--) {
	}
    var end = new Date;
    return end - start;
}

nopIteration1 : 38ms
function nopIteration1(a, f) {
    var start = new Date;
	var l = a.length;
    for (var i = 0; i &lt; l; i++){
	}
    var end = new Date;
    return end - start;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@olliej</p>
<p>Here are a few more that are a lot quicker<br />
<code><br />
nopIteration0 : 22ms<br />
function nopIteration0(a, f) {<br />
    var start = new Date;<br />
	var i = a.length;<br />
	while(i--) {<br />
	}<br />
    var end = new Date;<br />
    return end - start;<br />
}</p>
<p>nopIteration1 : 38ms<br />
function nopIteration1(a, f) {<br />
    var start = new Date;<br />
	var l = a.length;<br />
    for (var i = 0; i &lt; l; i++){<br />
	}<br />
    var end = new Date;<br />
    return end - start;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nosredna</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273054</link>
		<dc:creator>Nosredna</dc:creator>
		<pubDate>Thu, 23 Apr 2009 14:10:47 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273054</guid>
		<description>Yeah, the best choice depends on sparseness. In C, I almost never had arrays as sparse as ones I&#039;d use today in JS. Say I set arr[0] to &quot;tiny&quot; and arr[10000] to &quot;big&quot;. How big is the array? Two elements or 10001 elements? First time I ever read about sparse arrays was in reading about how the old spreadsheets were programmed (maybe a discussion of Visicalc in Byte magazine). It seemed so esoteric at the time, but it&#039;s something that just falls out of JavaScript&#039;s design. Most of the knocks I&#039;ve heard against JS on the array side is how slow it is with dense traditional arrays (treating arrays just like objects), but I think some of the new engines have really sped all this up.</description>
		<content:encoded><![CDATA[<p>Yeah, the best choice depends on sparseness. In C, I almost never had arrays as sparse as ones I&#8217;d use today in JS. Say I set arr[0] to &#8220;tiny&#8221; and arr[10000] to &#8220;big&#8221;. How big is the array? Two elements or 10001 elements? First time I ever read about sparse arrays was in reading about how the old spreadsheets were programmed (maybe a discussion of Visicalc in Byte magazine). It seemed so esoteric at the time, but it&#8217;s something that just falls out of JavaScript&#8217;s design. Most of the knocks I&#8217;ve heard against JS on the array side is how slow it is with dense traditional arrays (treating arrays just like objects), but I think some of the new engines have really sped all this up.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: geilp</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273053</link>
		<dc:creator>geilp</dc:creator>
		<pubDate>Thu, 23 Apr 2009 13:02:37 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273053</guid>
		<description>Yeah, great! :) I didn&#039;t know that with JavaScript 1.7 list comprehensions (and generators) were introduced. When I got serious with JavaScript I already have had a strong Python background and therefore really missed syntactical sugar like this.</description>
		<content:encoded><![CDATA[<p>Yeah, great! :) I didn&#8217;t know that with JavaScript 1.7 list comprehensions (and generators) were introduced. When I got serious with JavaScript I already have had a strong Python background and therefore really missed syntactical sugar like this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nene</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273052</link>
		<dc:creator>nene</dc:creator>
		<pubDate>Thu, 23 Apr 2009 12:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273052</guid>
		<description>@ywg: Array comprehensions are a lot easier to read than the current forEach() method where you have to supply a function which makes it fairly long. They might look cryptic at first sight, but that&#039;s because you aren&#039;t used to them.</description>
		<content:encoded><![CDATA[<p>@ywg: Array comprehensions are a lot easier to read than the current forEach() method where you have to supply a function which makes it fairly long. They might look cryptic at first sight, but that&#8217;s because you aren&#8217;t used to them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: olliej</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273051</link>
		<dc:creator>olliej</dc:creator>
		<pubDate>Thu, 23 Apr 2009 12:02:06 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273051</guid>
		<description>FirbyBoy: oh yeah, i know that (also object property iteration) i only really included it for the sake of reference.</description>
		<content:encoded><![CDATA[<p>FirbyBoy: oh yeah, i know that (also object property iteration) i only really included it for the sake of reference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TNO</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273050</link>
		<dc:creator>TNO</dc:creator>
		<pubDate>Thu, 23 Apr 2009 11:54:24 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273050</guid>
		<description>Don&#039;t forget JScripts Enumerator object:
&lt;b&gt;
var e = new Enumerator(Collection); 
for (;!e.atEnd();e.moveNext()){
   var x = e.item();
   ...
}&lt;/b&gt;</description>
		<content:encoded><![CDATA[<p>Don&#8217;t forget JScripts Enumerator object:<br />
<b><br />
var e = new Enumerator(Collection);<br />
for (;!e.atEnd();e.moveNext()){<br />
   var x = e.item();<br />
   &#8230;<br />
}</b></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FirbyBoy</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273049</link>
		<dc:creator>FirbyBoy</dc:creator>
		<pubDate>Thu, 23 Apr 2009 11:52:38 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273049</guid>
		<description>The idiom &quot;for ( .. in .. )&quot; is worth considering if the density of the array is very low as it only iterates over the values that have been defined. How you track the density of an array is another matter entirely.</description>
		<content:encoded><![CDATA[<p>The idiom &#8220;for ( .. in .. )&#8221; is worth considering if the density of the array is very low as it only iterates over the values that have been defined. How you track the density of an array is another matter entirely.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: olliej</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273048</link>
		<dc:creator>olliej</dc:creator>
		<pubDate>Thu, 23 Apr 2009 11:16:06 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273048</guid>
		<description>I just made a *really* ugly test case at http://nerget.com/jstests/performance/array-iteration.html to test them.  In short: avoid &#039;for(.. in ..)&#039; like the plague, but we already knew that :D</description>
		<content:encoded><![CDATA[<p>I just made a *really* ugly test case at <a href="http://nerget.com/jstests/performance/array-iteration.html" rel="nofollow">http://nerget.com/jstests/performance/array-iteration.html</a> to test them.  In short: avoid &#8216;for(.. in ..)&#8217; like the plague, but we already knew that :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aimos</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273047</link>
		<dc:creator>Aimos</dc:creator>
		<pubDate>Thu, 23 Apr 2009 10:53:30 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273047</guid>
		<description>The main question is, what is the fastest method.

Some benchmarks please.</description>
		<content:encoded><![CDATA[<p>The main question is, what is the fastest method.</p>
<p>Some benchmarks please.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ywg</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273046</link>
		<dc:creator>ywg</dc:creator>
		<pubDate>Thu, 23 Apr 2009 10:33:07 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273046</guid>
		<description>Why would one want to do a simple operation like iterating a collection in such a cryptic way ?
Array comprehension looks like maintenance hell.</description>
		<content:encoded><![CDATA[<p>Why would one want to do a simple operation like iterating a collection in such a cryptic way ?<br />
Array comprehension looks like maintenance hell.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: olliej</title>
		<link>http://ajaxian.com/archives/how-many-ways-can-you-iterate-over-an-array-in-javascript/comment-page-1#comment-273045</link>
		<dc:creator>olliej</dc:creator>
		<pubDate>Thu, 23 Apr 2009 09:46:22 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6643#comment-273045</guid>
		<description>It&#039;s worth noting the `for each`, comprehensions, and Iterators are not in any standard</description>
		<content:encoded><![CDATA[<p>It&#8217;s worth noting the `for each`, comprehensions, and Iterators are not in any standard</p>
]]></content:encoded>
	</item>
</channel>
</rss>

