<?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>Chapter 42 &#187; efficiency</title>
	<atom:link href="http://chapter42.whitewhale.net/tag/efficiency/feed/" rel="self" type="application/rss+xml" />
	<link>http://chapter42.whitewhale.net</link>
	<description></description>
	<lastBuildDate>Thu, 30 Sep 2010 17:20:47 +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>CSS optimization</title>
		<link>http://chapter42.whitewhale.net/2008/05/15/css-optimization/</link>
		<comments>http://chapter42.whitewhale.net/2008/05/15/css-optimization/#comments</comments>
		<pubDate>Thu, 15 May 2008 16:35:08 +0000</pubDate>
		<dc:creator>Donald</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[bestpractices]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://chapter42.whaleblogs.net/?p=20</guid>
		<description><![CDATA[I spoke briefly about our particular CSS formatting structure in my last post. But here&#8217;s something else that&#8217;s been on my mind a lot.
At White Whale, we&#8217;re of course always thinking about code efficiency: less queries, less code, fewer requests, et cetera. And much of this on the design side is rote best practices (semantics, [...]]]></description>
			<content:encoded><![CDATA[<p>I spoke briefly about our particular CSS formatting structure in <a href="http://chapter42.whaleblogs.net/?p=14">my last post</a>. But here&#8217;s something else that&#8217;s been on my mind a lot.</p>
<p>At White Whale, we&#8217;re of course always thinking about code efficiency: less queries, less code, fewer requests, et cetera. And much of this on the design side is rote best practices (semantics, accessibility, compatibility). But I still find myself sweating more nitty-gritty details, so I was fascinated to see <a href="http://jpsykes.com/152/testing-css-performance-pt-2">jpsykes&#8217; analysis</a> of this one. The question boils down to, &#8220;How specific should I get with CSS selectors?&#8221;<span id="more-20"></span></p>
<p>We have:<br />
<span class="code">#header #navigation .portal  { font-weight:bold; }</span><br />
vs.<br />
<span class="code">.portal { font-weight:bold; }</span><br />
I&#8217;ve leaned toward the first on a gut feeling; <span class="code">.portal</span> will only ever appear in <span class="code">#navigation</span> in <span class="code">#header</span>, so why not tell the browser where to look? But intuition is wrong in this case. Check out the graph (again, via <a href="http://jpsykes.com/152/testing-css-performance-pt-2">jpsykes</a>):<br />
<a href="http://www.whitewhale.net/wordpress/wp-content/uploads/2008/05/data-chart.png"><img class="alignnone size-full wp-image-21" title="CSS optimization chart" src="http://www.whitewhale.net/wordpress/wp-content/uploads/2008/05/data-chart.png" alt="CSS rendering times in browsers for two selector methods" width="500" height="375" /><br />
</a></p>
<p>Giving <em>more</em> selector specificity causes performance hits across all the major browsers. What&#8217;s happening is that the browser actually starts at the right-hand side of the selector and searches backwards. So it finds all instances of <span class="code">.portal</span>, then climbs up the DOM to make sure it&#8217;s in <span class="code">#navigation</span>, then filters again for <span class="code">#header</span>. (You&#8217;ll see that FF3 seems to perform best overall, but that&#8217;s a little misleading for reasons a commenter notes <a href="http://jpsykes.com/152/testing-css-performance-pt-2#comment-846">here</a>. Check out the results with <a href="http://jpsykes.com/153/more-css-performance-testing-pt-3">a better control</a>.)</p>
<p>Interestingly, the opposite effect is true for JS frameworks. Their selector implementations &#8212; we use jQuery at White Whale &#8212; tend to process the selectors from left-to-right: first finding <span class="code">#header</span>, then looking inside it for <span class="code">#navigation</span>, and then inside that for any instances of <span class="code">.portal</span>. So those work best when coded with some specificity.</p>
<p>It&#8217;s important to note here that we&#8217;re talking fractions of a second on a page with 20,000 test elements. So until we start serving 20,000 database records on a single page, our best practice will be whatever makes it easiest to keep the code straight in our own minds and for our clients &#8212; but I can&#8217;t help but think I&#8217;ll be subconsciously affected by the knowledge that every <span class="code">.newslist li .summary</span> might shaving precious milliseconds off our load time.</p>
]]></content:encoded>
			<wfw:commentRss>http://chapter42.whitewhale.net/2008/05/15/css-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

