<?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>Webitations &#187; jQuery</title>
	<atom:link href="http://ivorycity.com/blog/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivorycity.com/blog</link>
	<description>Web development tips, tricks, and topics</description>
	<lastBuildDate>Thu, 07 Jul 2011 17:43:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Efficient jQuery Selectors</title>
		<link>http://ivorycity.com/blog/2009/03/23/hello-world/</link>
		<comments>http://ivorycity.com/blog/2009/03/23/hello-world/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 00:12:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://ivorycity.com/blog/?p=1</guid>
		<description><![CDATA[jQuery is seriously awesome, but if we don&#8217;t use it carefully we can put a pretty serious load on the users&#8217; CPU.  This can mean a perceptible delay when the $(document).ready() function runs if there are loads of un-optimized selectors running. Considering that most users spend 4 seconds on a site, 1 or 2 seconds [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery is seriously awesome, but if we don&#8217;t use it carefully we can put a pretty serious load on the users&#8217; CPU.  This can mean a perceptible delay when the $(document).ready() function runs if there are loads of un-optimized selectors running. Considering that most users spend 4 seconds on a site, 1 or 2 seconds of delay can mean losing your user.  What kinds of jQuery selectors are fastest? Which ones are slower, and which ones must we avoid at all costs to ensure a responsive experience?</p>
<h4>Which is fastest?</h4>
<p>$(&#8217;.box_text&#8217;).html(&#8217;test1&#8242;);<br />
$(&#8217;span.box_text&#8217;).html(&#8217;test2&#8242;);<br />
$(&#8217;#box_text&#8217;).html(&#8217;test3&#8242;);</p>
<p>Which is fastest? Have a look for yourself on this test page, <a title="test0" href="http://www.ivorycity.com/tests/jquery/selectors/test0.php" target="_blank">http://www.ivorycity.com/tests/jquery/selectors/test0.php</a>.</p>
<p>If you&#8217;re using FF3, they should all be about the same, but if you&#8217;re using IE6,7, or FF2, $(&#8217;#box_text&#8217;) is up to 10x faster than $(&#8217;.box_text&#8217;)!</p>
<h4>Why is this?</h4>
<p>In order to answer these questions, we need to know a little about how jQuery finds DOM nodes.</p>
<p>Internally, jQuery uses document.getElementById() to find elements based on id. Browsers index id&#8217;s internally, so the lookup is instantaneous.  Meanwhile,  using $(&#8217;.someclass&#8217;) in IE causes jQuery to iterate over every element in the DOM to find one with a particular class &#8211; that&#8217;s crazy slow!</p>
<p>By prefixing &#8217;span&#8217; to the query, we get $(&#8217;span.box_text&#8217;) &#8211; this is a little better because internally, jQuery calls document.getElementsByTagName and iterates the results. Since spans are only a sub-set of all DOM nodes, the query is a lot faster. So if you&#8217;re performing lots of &#8216;naked&#8217; class selectors, you&#8217;re going to consume a lot of user cpu.</p>
<p>In Firefox 3 however,  it&#8217;s no longer the case that $(&#8217;.someclass&#8217;) causes an iteration over the DOM; document.getElementsByClassName() was added in FF3 to index element class names inside the browser memory space, which is much, much faster. That&#8217;s why we don&#8217;t see the same performance hit as in IE.</p>
<p>Now, it may not always be appropriate to access elements by id. If you&#8217;re trying to find many instances, say 100+, with a particular class name, it&#8217;s actually faster to use $(&#8217;.someclass&#8217;) than to loop through a set of id&#8217;s.</p>
<h4>The moral of the story&#8230;</h4>
<p>1) Use id selectors to reference a &#8216;needle in a haystack&#8217;</p>
<p>2) Use class selectors prudently, and only when referencing large numbers of DOM nodes</p>
<p>3) You can also prevent iteration over the entire dom by doing something like $(&#8217;#myid .myclass&#8217;) which only searches sub-nodes of #myid.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivorycity.com/blog/2009/03/23/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

