<?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>Web 3.0, 6 Bladed Razors, 7 Minute Abs &#187; Performance</title>
	<atom:link href="http://www.zachleat.com/web/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zachleat.com/web</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 17:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Quick Performance Tip: jQuery and addClass</title>
		<link>http://www.zachleat.com/web/quick-performance-tip-jquery-and-addclass/</link>
		<comments>http://www.zachleat.com/web/quick-performance-tip-jquery-and-addclass/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 04:51:01 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/?p=226</guid>
		<description><![CDATA[Abstractions are helpful and dangerous. But the more we know about a library&#8217;s internals, the less danger we&#8217;ll be in later. Here&#8217;s an issue I ran into where I had assumed that jQuery would be optimized for this case, but it wasn&#8217;t. I&#8217;ll go over my bad assumption and how to workaround it. As of [...]]]></description>
			<content:encoded><![CDATA[<p>Abstractions are helpful and dangerous.  But the more we know about a library&#8217;s internals, the less danger we&#8217;ll be in later.  Here&#8217;s an issue I ran into where I had assumed that jQuery would be optimized for this case, but it wasn&#8217;t.  I&#8217;ll go over my bad assumption and how to workaround it.</p>
<p>As of jQuery 1.3.2, adding multiple HTML classes to an element using jQuery&#8217;s addClass method will add them one at a time, modifying the className property of an element for each class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myElement'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myFirstClass mySecondClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s the original code inside of jQuery 1.3.2.  Note how the classNames string is split, and elem.className is changed for each split entry.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">add<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> elem<span style="color: #339933;">,</span> classNames <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>classNames <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s+/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> className<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> elem.<span style="color: #660066;">nodeType</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>jQuery.<span style="color: #660066;">className</span>.<span style="color: #660066;">has</span><span style="color: #009900;">&#40;</span> elem.<span style="color: #660066;">className</span><span style="color: #339933;">,</span> className <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
            elem.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">className</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> className<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #006600; font-style: italic;">// ...</span></pre></div></div>

<p>This may cause longer than needed delays, as reflow may occur after every class is added individually.  If absolutely necessary, you can always fall back to modifying the className yourself, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myElement'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">' myFirstClass mySecondClass'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Most likely, this isn&#8217;t a tip that will be needed, but it is useful to be aware of.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/quick-performance-tip-jquery-and-addclass/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Faster YUI DataTable with 5 Lines of Code</title>
		<link>http://www.zachleat.com/web/faster-yui-datatable-with-5-lines-of-code/</link>
		<comments>http://www.zachleat.com/web/faster-yui-datatable-with-5-lines-of-code/#comments</comments>
		<pubDate>Fri, 28 Dec 2007 00:34:56 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DataTable]]></category>
		<category><![CDATA[Grid]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/12/27/faster-yui-datatable-with-5-lines-of-code/</guid>
		<description><![CDATA[Holy Reflows Batman! The typical usage of a DataTable in the Yahoo User Interface JavaScript library involves passing a string into the constructor signifying the ID attribute of the container you want to attach the DataTable to. However, the YUI DataTable loves the DOM and creating nodes individually using DOM methods. Normally that&#8217;d be fine, [...]]]></description>
			<content:encoded><![CDATA[<p>Holy Reflows Batman!  The typical usage of a DataTable in the Yahoo User Interface JavaScript library involves passing a string into the constructor signifying the ID attribute of the container you want to attach the DataTable to.  However, the YUI DataTable loves the DOM and creating nodes individually using DOM methods.  Normally that&#8217;d be fine, but one of the first things it does in the constructor is create the table element and attach it to the live DOM.  This is a no-no.  Now, every time they append a new node (for a new row or a new cell inside of a row), it causes a reflow in the browser!  What does this mean?  Really bad lag when you insert 40 or 50 rows.  Recognize this piece of code?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myDataTable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #660066;">widget</span>.<span style="color: #660066;">DataTable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myContainer&quot;</span><span style="color: #339933;">,</span> myColumnDefs<span style="color: #339933;">,</span> myDataSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Straight from the docs.  No no no!</p>
<p>Instead, you should pass in an unattached DOM node instead of a string!</p>
<p>Try this code on for size:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myDataTable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #660066;">widget</span>.<span style="color: #660066;">DataTable</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> myColumnDefs<span style="color: #339933;">,</span> myDataSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
myDataTable.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'initEvent'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myContainer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// CHANGE THIS -- match the id of the container you want.</span>
    <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>d.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> d.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>d.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// remove previous DataTables</span>
    d.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._elContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/faster-yui-datatable-with-5-lines-of-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

