<?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: Optimizing JavaScript (part 1)</title>
	<atom:link href="http://www.shamasis.net/2009/04/optimizing-javascript-1/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/</link>
	<description>All you need to know what I know</description>
	<lastBuildDate>Sun, 06 Jun 2010 06:42:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Shamasis Bhattacharya</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-278</link>
		<dc:creator>Shamasis Bhattacharya</dc:creator>
		<pubDate>Tue, 26 May 2009 19:31:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-278</guid>
		<description>&quot;===&quot; may not be a desirable operator for every situation. In case of switch statement, the switching variable is compared for existence only once. Whereas, in case of if-else block, both sides of the &quot;==&quot; operator is checked for existence and that, to some extent, adds up to the execution time.

Now, with an if-else block with &quot;===&quot; operator, the above logic theoretically appears to have been overcome. But, apparently it is not! I did run a test of 100000 iterations with 20 cases and if-else per loop and the result still shows that switch-case is faster and that &quot;==&quot; or &quot;===&quot; has almost no difference in execution time. (&lt;a href=&quot;http://www.shamasis.net/wp-content/uploads/2009/04/switch-case-and-if-else-test.html&quot; rel=&quot;nofollow&quot;&gt;View Testing Script&lt;/a&gt;)

And ah! The link you mentioned is really worthwhile. It really has some extensive validation of the above points. :)</description>
		<content:encoded><![CDATA[<p>&#8220;===&#8221; may not be a desirable operator for every situation. In case of switch statement, the switching variable is compared for existence only once. Whereas, in case of if-else block, both sides of the &#8220;==&#8221; operator is checked for existence and that, to some extent, adds up to the execution time.</p>
<p>Now, with an if-else block with &#8220;===&#8221; operator, the above logic theoretically appears to have been overcome. But, apparently it is not! I did run a test of 100000 iterations with 20 cases and if-else per loop and the result still shows that switch-case is faster and that &#8220;==&#8221; or &#8220;===&#8221; has almost no difference in execution time. (<a href="http://www.shamasis.net/wp-content/uploads/2009/04/switch-case-and-if-else-test.html" rel="nofollow">View Testing Script</a>)</p>
<p>And ah! The link you mentioned is really worthwhile. It really has some extensive validation of the above points. <img src='http://www.shamasis.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nico</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-277</link>
		<dc:creator>nico</dc:creator>
		<pubDate>Tue, 26 May 2009 12:30:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-277</guid>
		<description>In your &quot;if, else if&quot; block, you compare the value with operator == which converts type of data. Lots of web sites advise the use of ===.
I think a switch statement is performing a comparison same way than ==. So are you sure it would be quicker than the &quot;if, else is&quot; block with operator === ?
For a great benchmark on the loops performance, there is :
http://blogs.sun.com/greimer/entry/best_way_to_code_a
which shows how bad is the &quot;for (var i in array)&quot; !!</description>
		<content:encoded><![CDATA[<p>In your &#8220;if, else if&#8221; block, you compare the value with operator == which converts type of data. Lots of web sites advise the use of ===.<br />
I think a switch statement is performing a comparison same way than ==. So are you sure it would be quicker than the &#8220;if, else is&#8221; block with operator === ?<br />
For a great benchmark on the loops performance, there is :<br />
<a href="http://blogs.sun.com/greimer/entry/best_way_to_code_a" rel="nofollow">http://blogs.sun.com/greimer/entry/best_way_to_code_a</a><br />
which shows how bad is the &#8220;for (var i in array)&#8221; !!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shamasis Bhattacharya</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-265</link>
		<dc:creator>Shamasis Bhattacharya</dc:creator>
		<pubDate>Fri, 01 May 2009 07:37:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-265</guid>
		<description>Well... I think that using a language profiler to track execution times is another alternative. For JavaScript, a good profiler is &lt;a href=&quot;http://getfirebug.com/js.html&quot; rel=&quot;nofollow&quot;&gt;FireBug.&lt;/a&gt; Other than that, there is &lt;a href=&quot;http://www.whitefrost.com/documents/html/technical/dhtml/jsprof.html&quot; rel=&quot;nofollow&quot;&gt;JS Profiler 2.o&lt;/a&gt;.
 
Using a profiler is a more robust and technically sound way to track memory leak and performance of your applications. I will soon write a section enunciating the benefits of using a profiler.</description>
		<content:encoded><![CDATA[<p>Well&#8230; I think that using a language profiler to track execution times is another alternative. For JavaScript, a good profiler is <a href="http://getfirebug.com/js.html" rel="nofollow">FireBug.</a> Other than that, there is <a href="http://www.whitefrost.com/documents/html/technical/dhtml/jsprof.html" rel="nofollow">JS Profiler 2.o</a>.</p>
<p>Using a profiler is a more robust and technically sound way to track memory leak and performance of your applications. I will soon write a section enunciating the benefits of using a profiler.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rahul B.</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-263</link>
		<dc:creator>Rahul B.</dc:creator>
		<pubDate>Fri, 01 May 2009 06:01:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-263</guid>
		<description>One way how you can check your execution time on javascript is using date tag, see the example below:

&lt;pre&gt;&lt;script type=&quot;text/javascript&quot;&gt;
function check_processing_time(){
  var start_time,end_time;
    start_time = new Date().getTime();
    FunctionName();
    end_time=new Date().getTime();
alert(&#039;Elapsed time: &#039;+((endTime-startTime)/1000)+&#039; seconds.&#039;);
}
&lt;/script&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>One way how you can check your execution time on javascript is using date tag, see the example below:</p>
<p><pre>&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;
function check_processing_time(){
&nbsp;&nbsp;var start_time,end_time;
&nbsp;&nbsp;&nbsp;&nbsp;start_time = new Date().getTime();
&nbsp;&nbsp;&nbsp;&nbsp;FunctionName();
&nbsp;&nbsp;&nbsp;&nbsp;end_time=new Date().getTime();
alert(&#039;Elapsed time: &#039;+((endTime-startTime)/1000)+&#039; seconds.&#039;);
}
&amp;lt;/script&amp;gt;</pre></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shamasis Bhattacharya</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-262</link>
		<dc:creator>Shamasis Bhattacharya</dc:creator>
		<pubDate>Wed, 29 Apr 2009 20:07:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-262</guid>
		<description>Oops! Typo there! i- should be i&#8722;&#8722;! (You see, Windows Live Writer automatically converted -- to a longer dash!)
My statement &lt;em&gt;:
&lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw2&quot;&gt;var&lt;/span&gt; i = &lt;span class=&quot;nu0&quot;&gt;10000&lt;/span&gt;; i; i&#8722;&#8722;&lt;span class=&quot;br0&quot;&gt;) &lt;/span&gt;&lt;/em&gt;&lt;span class=&quot;br0&quot;&gt;is equivalent to&lt;/span&gt;&lt;em&gt;
&lt;/em&gt;&lt;em&gt;&lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw2&quot;&gt;var&lt;/span&gt; i = &lt;span class=&quot;nu0&quot;&gt;10000&lt;/span&gt;; i==0; i&#8722;&#8722;&lt;span class=&quot;br0&quot;&gt;)&lt;/span&gt;&lt;/em&gt; &lt;span class=&quot;br0&quot;&gt;&lt;/span&gt;&lt;em&gt;&lt;span class=&quot;br0&quot;&gt;&lt;/span&gt;&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>Oops! Typo there! i- should be i&minus;&minus;! (You see, Windows Live Writer automatically converted &#8212; to a longer dash!)<br />
My statement <em>:<br />
<span class="kw1">for</span><span class="br0">(</span><span class="kw2">var</span> i = <span class="nu0">10000</span>; i; i&minus;&minus;<span class="br0">) </span></em><span class="br0">is equivalent to</span><em><br />
</em><em><span class="kw1">for</span><span class="br0">(</span><span class="kw2">var</span> i = <span class="nu0">10000</span>; i==0; i&minus;&minus;<span class="br0">)</span></em> <span class="br0"></span><em><span class="br0"></span></em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dhruva</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-261</link>
		<dc:creator>Dhruva</dc:creator>
		<pubDate>Wed, 29 Apr 2009 19:01:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-261</guid>
		<description>(var i = 10000; i&gt;0; i––)
is what you mean to say, right?</description>
		<content:encoded><![CDATA[<p>(var i = 10000; i>0; i––)<br />
is what you mean to say, right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy L</title>
		<link>http://www.shamasis.net/2009/04/optimizing-javascript-1/comment-page-1/#comment-260</link>
		<dc:creator>Andy L</dc:creator>
		<pubDate>Sat, 25 Apr 2009 20:54:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.shamasis.net/?p=515#comment-260</guid>
		<description>Damn! I could never believe that counting down in a loop works... and guess what you actually made me write a test code to try it out.


It worked in FF and Chrome but very little difference in IE. Even then... it works.</description>
		<content:encoded><![CDATA[<p>Damn! I could never believe that counting down in a loop works&#8230; and guess what you actually made me write a test code to try it out.</p>
<p>It worked in FF and Chrome but very little difference in IE. Even then&#8230; it works.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
