<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Claudio&#039;s Hideout &#187; performance</title>
	<atom:link href="http://claudio.cicali.name/post/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://claudio.cicali.name</link>
	<description>Claudio Cicali web hub</description>
	<lastBuildDate>Wed, 08 Sep 2010 07:45:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery vs document.getElementById</title>
		<link>http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/</link>
		<comments>http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 16:55:30 +0000</pubDate>
		<dc:creator>claudio</dc:creator>
				<category><![CDATA[Articoli]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Review]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://claudio.cicali.name/?p=601</guid>
		<description><![CDATA[With the first post of the year, I&#8217;d like to present the results of a (very) simple and unreliable performance benchmark.
The question I would like to give an answer to was: giving the selector of an HTML ID, how much the document.getElementById is faster than the jQuery selection? Well &#8211; as it tourned out &#8211; &#8230; <span class='readmore'>[<a href='http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/'>continua &#187;</a>]</span>]]></description>
			<content:encoded><![CDATA[<p>With the first post of the year, I&#8217;d like to present the results of a (very) simple and unreliable performance benchmark.</p>
<p>The question I would like to give an answer to was: giving the selector of an HTML ID, how much the document.getElementById is faster than the jQuery selection? Well &#8211; as it tourned out &#8211; it is astonishingly faster.</p>
<p>The test run over 100000 calls to the same minimal function and then collect the result in millisecond. For each test, 8 runs were made and the greatest and lowest value were dropped before the average time was computed. Every test was ran against Firefox 3.5.6, Firefox 3.6b5, Chrome 4, Safari 4, IE 8 and Opera 10.10.</p>
<p>&#8220;Why don&#8217;t run the test also against the soon to be released jQuery 1.4.0?&#8221;, I asked myself. So I did.</p>
<p>The test code itself goes along these lines:</p>
<pre class="brush: jscript;">
function benchmark(f, result) {
  var start = new Date();
  f();
  var stop = new Date();
  document.getElementById(result).innerHTML = stop - start;
}

$(&quot;#start&quot;).click(function() {
    benchmark(function() {
      var e;
      for(n=0; n &lt; 100000; n++) {
        e = $('#testID');
// or  e = document.getElementById('#testID');
      }
    }, &quot;result&quot;);
    return false;
});
</pre>
<p>And here comes the whole picture:</p>
<p style="text-align: center;"><a href="http://claudio.cicali.name/wp-content/uploads/2010/01/jqueryspeed.jpg"><img class="aligncenter size-medium wp-image-602" title="jqueryspeed" src="http://claudio.cicali.name/wp-content/uploads/2010/01/jqueryspeed.jpg" alt="" /></a></p>
<p style="text-align: left;">What did I learn?</p>
<ul>
<li>when possible, use the document.getElementById() native function (or a simple helper wrapper) instead of the jQuery method</li>
<li>IE and Firefox 3.5 are embarassingly slow</li>
<li>Chrome and Safari are embarassingly fast</li>
<li>Firefox 3.6 is gaining a lot of performance points, but still&#8230;</li>
<li>Opera 10.10 is good, but the gEBI performances are very bad</li>
<li>Chrome is loosing something with jQuery 1.4. Well, it seems so. I don&#8217;t think this is real.</li>
<li>The best performance gain with jQuery 1.4 would be for IE8</li>
</ul>
<p>Remember that this test is extremely unreliable and of indicative quality only. Do not take any assumption for granted :)</p>
<p><strong>ADDENDUM</strong>: as a follow up to a <a href="http://friendfeed.com/caludio/ef460e48/jquery-vs-document-getelementbyid">discussion on Friendfeed</a>, I ran one more test. This time I&#8217;ve used a wrapper like this one:</p>
<pre class="brush: jscript;">

function $getId(id) {
  $(document.getElementById(id));
}

$(&quot;#startWRAPPER&quot;).click(function() {
    benchmark(function() {
      var e;
      for(n=0; n &lt; 100000; n++) {
        e = $getId('testID');
      }
    }, &quot;result&quot;);
    return false;
});
</pre>
<p>When you need an ID, as a jQuery object, using the wrapper can certainly give you same benefits. The test has been ran with the latest jQuery 1.4 version.</p>
<p><a href="http://claudio.cicali.name/wp-content/uploads/2010/01/jqueryspeed22.jpg"><img class="aligncenter size-full wp-image-613" title="jqueryspeed2" src="http://claudio.cicali.name/wp-content/uploads/2010/01/jqueryspeed22.jpg" alt="" width="428" height="168" /></a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/&amp;title=jQuery+vs+document.getElementById" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/&amp;title=jQuery+vs+document.getElementById" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=jQuery+vs+document.getElementById+-+&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/&amp;t=jQuery+vs+document.getElementById" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=jQuery+vs+document.getElementById&amp;link=http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fclaudio.cicali.name%2Fpost%2F2010%2F01%2Fjquery-vs-document-getelementbyid%2F&amp;t=jQuery+vs+document.getElementById" rel="nofollow" class="external" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://claudio.cicali.name/post/2010/01/jquery-vs-document-getelementbyid/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->