<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.yuiblog.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Yahoo! User Interface Blog » Performance</title>
	
	<link>http://www.yuiblog.com/blog</link>
	<description>News and Articles about Designing and Developing with Yahoo! Libraries.</description>
	<lastBuildDate>Mon, 06 Jul 2009 22:28:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.yuiblog.com/yuiblog/performance" type="application/rss+xml" /><item>
		<title>YUI Theater — Douglas Crockford: "Ajax Performance"</title>
		<link>http://www.yuiblog.com/blog/2008/12/23/video-crockford-performance/</link>
		<comments>http://www.yuiblog.com/blog/2008/12/23/video-crockford-performance/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 16:07:47 +0000</pubDate>
		<dc:creator>Eric Miraglia</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[YUI Theater]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/12/23/video-crockford-performance/</guid>
		<description><![CDATA[
Douglas Crockford returns to YUI Theater with another chapter in his evolving lecture series.  This session, &#8220;Ajax Performance,&#8221; debunks common misconceptions about the relationship between JavaScript and performance and gives engineers a core focus for improving the performance of web apps: Reduce the value of n.  Because DOM interactions are generally slow, leveraging [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://video.yahoo.com/watch/4156174/11192533"><img src="http://ericmiraglia.com/assets/crockford-performance2.jpg" width="510" height="306" /></a></p>
<p>Douglas Crockford returns to YUI Theater with another chapter in his evolving lecture series.  This session, &#8220;Ajax Performance,&#8221; debunks common misconceptions about the relationship between JavaScript and performance and gives engineers a core focus for improving the performance of web apps: <em>Reduce the value of </em>n.  Because DOM interactions are generally slow, leveraging Ajax to reduce the number of DOM operations, Douglas argues, is often the most important optmization you can make.  In fact, it usually dwarfs other techniques in terms of its impact on the actual experience of using a website.</p>
<p>This talk joins <a href="http://developer.yahoo.com/yui/theater/">an extensive library of Douglas&#8217;s lectures now available on YUI Theater</a>, including his popular series on JavaScript.</p>
<ul>
<li><a href="http://yuiblog.com/assets/crockford-performance.zip">Slides (zipped PPT)</a></li>
<li><a href="http://ericmiraglia.com/blog/?p=140">Full text transcript of this talk</a></li>
</ul>
<p><object width="512" height="322"><param name="movie" value="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.30" /><param name="allowFullScreen" value="true" /><param name="AllowScriptAccess" VALUE="always" /><param name="bgcolor" value="#000000" /><param name="flashVars" value="id=11157560&#038;vid=4141759&#038;lang=en-us&#038;intl=us&#038;thumbUrl=http%3A//us.i1.yimg.com/us.yimg.com/p/i/bcst/videosearch/6543/76907810.jpeg&#038;embed=1" /><embed src="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.30" type="application/x-shockwave-flash" width="512" height="322" allowFullScreen="true" AllowScriptAccess="always" bgcolor="#000000" flashVars="id=11157560&#038;vid=4141759&#038;lang=en-us&#038;intl=us&#038;thumbUrl=http%3A//us.i1.yimg.com/us.yimg.com/p/i/bcst/videosearch/6543/76907810.jpeg&#038;embed=1" ></embed></object></p>
<p><a href="http://video.yahoo.com/watch/4141759/11157560">Douglas Crockford: &quot;Ajax Performance&quot;</a> @ <a href="http://video.yahoo.com" >Yahoo! Video</a></p>
<p><img src="http://us.i1.yimg.com/us.yimg.com/i/nt/ic/ut/bsc/vidcam12_1.gif" border="0" hspace="10"><a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/crockford-performance.m4v">download (m4v)</a></p>
<h3>In Case You Missed&#8230;</h3>
<p>Some other recent videos from the <a href="http://developer.yahoo.com/yui/theater/">YUI Theater series</a>:</p>
<ul>
<li><strong>Nicole Sullivan:</strong> &quot;Design Fast Websites (Don&#8217;t Blame the Rounded Corners)&quot; (<a href="http://video.yahoo.com/watch/4156174/11192533">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/sullivan-fast.m4v">.m4v download</a>)</li>
<li><strong>Todd Kloots:</strong> &#8220;Developing Accessible Widgets Using ARIA&#8221; (<a href="http://video.yahoo.com/watch/4073211/10996186">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/kloots-aria.m4v">.m4v download</a>)</li>
<li><strong>Nicholas C. Zakas:</strong> &quot;Test-Driven Development with YUI Test&quot; (<a href="http://video.yahoo.com/watch/3737228/10267335">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/zakas-yuitest.m4v">.m4v download</a>)</li>
<li><strong>Douglas Crockford:</strong> &quot;Web Forward&quot; (<a href="http://video.yahoo.com/watch/3730137/10250950">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/crockford-webforward.m4v">.m4v download</a>)</li>
</ul>
<h3>Subscribing to YUI Theater:</h3>
<ul>
<li><a href="http://feeds.feedburner.com/yuiblog/yui-theater">YUI Theater RSS feed</a></li>
<li><a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=263846173&amp;s=143441">YUI Theater on iTunes</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/12/23/video-crockford-performance/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/crockford-performance.m4v" length="195027943" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/sullivan-fast.m4v" length="173150421" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/kloots-aria.m4v" length="123775849" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/zakas-yuitest.m4v" length="149378402" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/crockford-webforward.m4v" length="144845803" type="video/x-m4v" />
		</item>
		<item>
		<title>YUI Theater — Nicole Sullivan: "Design Fast Websites (Don’t Blame the Rounded Corners)"</title>
		<link>http://www.yuiblog.com/blog/2008/12/23/video-sullivan/</link>
		<comments>http://www.yuiblog.com/blog/2008/12/23/video-sullivan/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 16:04:25 +0000</pubDate>
		<dc:creator>Eric Miraglia</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[YUI Theater]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/12/23/video-sullivan/</guid>
		<description><![CDATA[
Nicole Sullivan is a website performance specialist and a former member of Yahoo&#8217;s Exceptional Performance Team. She is currently writing a book for O&#8217;Reilly with Stoyan Stefanov on performance optimization and she and Stoyan are the creators of Smushit, an engine for crushing images.
Nicole visited Yahoo last week to do an encore of her &#34;Design [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://video.yahoo.com/watch/4156174/11192533"><img src="http://ericmiraglia.com/assets/sullivan-fast.jpg" width="510" height="290" /></a></p>
<p><a href="http://stubbornella.org">Nicole Sullivan</a> is a website performance specialist and a former member of Yahoo&#8217;s Exceptional Performance Team. She is currently writing a book for O&#8217;Reilly with Stoyan Stefanov on performance optimization and she and Stoyan are the creators of <a href="http://smushit.com">Smushit</a>, an engine for crushing images.</p>
<p>Nicole visited Yahoo last week to do an encore of her &quot;Design Fast Websites&quot; talk in which she outlines a set of practical guidelines for building websites that are supremely fast <em>and</em> visually rich. Her advice is to know your craft, to engage your designers, and to make sure that your collaboration with designers works intelligently in the service of users. She was kind enough to let us record the talk and share it with you here on <a href="http://developer.yahoo.com/yui/theater/">YUI Theater</a>.</p>
<ul>
<li><a href="http://www.slideshare.net/stubbornella/designing-fast-websites-presentation">Slides (on Slideshare)</a></li>
</ul>
<p><object width="512" height="322"><param name="movie" value="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.30" /><param name="allowFullScreen" value="true" /><param name="AllowScriptAccess" VALUE="always" /><param name="bgcolor" value="#000000" /><param name="flashVars" value="id=11192533&#038;vid=4156174&#038;lang=en-us&#038;intl=us&#038;thumbUrl=http%3A//us.i1.yimg.com/us.yimg.com/p/i/bcst/videosearch/6581/77038678.jpeg&#038;embed=1" /><embed src="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.30" type="application/x-shockwave-flash" width="512" height="322" allowFullScreen="true" AllowScriptAccess="always" bgcolor="#000000" flashVars="id=11192533&#038;vid=4156174&#038;lang=en-us&#038;intl=us&#038;thumbUrl=http%3A//us.i1.yimg.com/us.yimg.com/p/i/bcst/videosearch/6581/77038678.jpeg&#038;embed=1" ></embed></object></p>
<p><a href="http://video.yahoo.com/watch/4156174/11192533">Nicole Sullivan: &quot;Design Fast Websites&quot;</a> @ <a href="http://video.yahoo.com" >Yahoo! Video</a></</p>
<p><img src="http://us.i1.yimg.com/us.yimg.com/i/nt/ic/ut/bsc/vidcam12_1.gif" border="0" hspace="10"><a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/sullivan-fast.m4v">download (m4v)</a></p>
<h3>In Case You Missed&#8230;</h3>
<p>Some other recent videos from the <a href="http://developer.yahoo.com/yui/theater/">YUI Theater series</a>:</p>
<ul>
<li><strong>Todd Kloots:</strong> &#8220;Developing Accessible Widgets Using ARIA&#8221; (<a href="http://video.yahoo.com/watch/4073211/10996186">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/kloots-aria.m4v">.m4v download</a>)</li>
<li><strong>Nicholas C. Zakas:</strong> &quot;Test-Driven Development with YUI Test&quot; (<a href="http://video.yahoo.com/watch/3737228/10267335">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/zakas-yuitest.m4v">.m4v download</a>)</li>
<li><strong>Douglas Crockford:</strong> &quot;Web Forward&quot; (<a href="http://video.yahoo.com/watch/3730137/10250950">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/crockford-webforward.m4v">.m4v download</a>)</li>
<li><strong>Eric Miraglia and Matt Sweeney:</strong> &quot;YUI3 &mdash; A Look Ahead&quot; (<a href="http://video.yahoo.com/watch/3711767/10207432">Yahoo! Video</a> | <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/miraglia-yui3.m4v">.m4v download</a>)</li>
</ul>
<h3>Subscribing to YUI Theater:</h3>
<ul>
<li><a href="http://feeds.feedburner.com/yuiblog/yui-theater">YUI Theater RSS feed</a></li>
<li><a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=263846173&amp;s=143441">YUI Theater on iTunes</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/12/23/video-sullivan/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/sullivan-fast.m4v" length="173150421" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/kloots-aria.m4v" length="123775849" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/zakas-yuitest.m4v" length="149378402" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/crockford-webforward.m4v" length="144845803" type="video/x-m4v" />
<enclosure url="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/yui/theater/miraglia-yui3.m4v" length="113033117" type="video/x-m4v" />
		</item>
		<item>
		<title>Image Optimization, Part 5: AlphaImageLoader</title>
		<link>http://www.yuiblog.com/blog/2008/12/08/imageopt-5/</link>
		<comments>http://www.yuiblog.com/blog/2008/12/08/imageopt-5/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 13:48:09 +0000</pubDate>
		<dc:creator>Stoyan Stefanov</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/12/08/imageopt-5/</guid>
		<description><![CDATA[

About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript.


This is part 5 in an ongoing series. You can read the other parts [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<div class="intro">
<p><img src="http://yuiblog.com/assets/stoyan.jpg" alt="Stoyan Stefanov." align="right" hspace="10" vspace="5"><em><strong>About the Author:</strong><a href="http://www.phpied.com"> Stoyan Stefanov</a> is a Yahoo! web developer working for the <a href="http://developer.yahoo.com/performance/">Exceptional Performance</a> team and leading the development of the <a href="http://developer.yahoo.com/yslow/">YSlow</a> performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">Object-Oriented JavaScript</a>.</em></p>
</div>
</div>
<p><em>This is part 5 in an ongoing series. You can read the other parts here:</em></p>
<ul>
<li><a href="http://yuiblog.com/blog/2008/10/29/imageopt-1/">Image Optimization Part 1: The Importance of Images</a></li>
<li><a href="http://yuiblog.com/blog/2008/11/04/imageopt-2/">Image Optimization Part 2: Selecting the Right File Format</a></li>
<li><a href="http://yuiblog.com/blog/2008/11/14/imageopt-3/">Image Optimization Part 3: Four Steps to File Size Reduction</a></li>
<li><a href="http://yuiblog.com/blog/2008/12/05/imageopt-4/">Image Optimization Part 4: Progressive JPEG&#8230;Hot or Not?</a></li>
</ul>
<p>This installment of the image optimization series is about the IE-proprietary AlphaImageLoader CSS filter, which developers often use as a workaround to solve transparency issues with truecolor PNGs in IE. The problem with AlphaImageLoader is that it hurts page performance and, therefore, hurts user experience.  I argue that AlphaImageLoader should be avoided when at all possible.</p>
<h3>Quick Refresher</h3>
<p>As mentioned in a <a href="http://yuiblog.com/blog/2008/11/04/imageopt-2/">previous article</a>, <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics">PNGs</a> come in several different types but can roughly be divided into:</p>
<ul>
<li>Indexed (palette), also referred to as PNG8 which have up to 256 colors.</li>
<li>Truecolor PNG, also referred to as PNG32 or PNG24.</li>
</ul>
<p>Both formats support alpha (variable) transparency and, while PNG8 images degrade to a GIF-like non-variable transparency in IE6 (<a href="http://yuiblog.com/assets/png8-transparency.png">example</a>, <a href="http://www.sitepoint.com/blogs/2007/09/18/png8-the-clear-winner/">source</a>), truecolor PNGs show an uglyish background in place of the transparent pixels (source <a href=""http://www.w3.org/Graphics/PNG/inline-alpha.html>W3C</a>).</p>
<p><img src="http://yuiblog.com/assets/png-transparency.png" alt="truecolor PNG transparency problem in IE6" /></p>
<h3>The AlphaImageLoader fix</h3>
<p>IE6 (and older versions of IE) provides a solution to the problem through its proprietary <code>filter</code> CSS property. The following code will display the proper image cross-browser:</p>
<pre>
#some-element {
    background: url(image.png);
    _background: none;
    _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.png', sizingMethod='crop');
}
</pre>
<p>As you can see, the underscore CSS hack is used to target IE &lt; 7 and</p>
<ol>
<li>&#8220;undo&#8221; the background; and</li>
<li>load the same image, using Microsoft&#8217;s AlphaImageLoader filter.</li>
</ol>
<p>The reason to target IE prior to version 7 is that IE7 supports the alpha transparency natively without the need for filters. (IE8 does too and it actually <a href="http://blogs.msdn.com/ie/archive/2008/09/08/microsoft-css-vendor-extensions.aspx">changes the filter syntax</a> completely.)</p>
<p>
It&#8217;s interesting to note that the filter doesn&#8217;t change the image; rather, it changes the HTML element this style is applied to. The other interesting thing is that each element is processed synchronously in a single UI thread. The process applying the filter takes some resources for each element and the more &#8220;filtered&#8221; elements you have, the worse it gets, even if you use the same image for all the elements.
</p>
<p>The question is: How does this affect the overall performance of the page?</p>
<h3>Freeze! Side Effect #1</h3>
<p>Here&#8217;s a simple experiment: Create a page that has a CSS filter and then simulate (and exaggerate) network latency by delaying the image used in the filter by ten seconds. The result? Not only is nothing rendered (blank page) for ten seconds, but the browser freezes, meaning you cannot interact with it, click its icons or menus, type in the URL&#8230;you can&#8217;t even close it.</p>
<p><a href="http://yuiblog.com/assets/image-opt-tests/alphatest.html">Here&#8217;s a test example.</a></p>
<p>In the example, I didn&#8217;t use the underscore hack so you can see the (d)effect in IE7 too, even in IE8 in &#8220;compatibility mode&#8221;.</p>
<p>While the effect is exaggerated for demo purposes, network latencies are a fact of life and this is probably the worst user experience you can deliver: Someone comes to your page and their browser freezes.</p>
<p>Note that parallel downloads are not blocked. The browser still downloads the other page components in the background, but there&#8217;s no progressive rendering. You can think of it this way &mdash; since IE will not render anything until the very last bit of CSS comes down the wire (<a href="http://www.phpied.com/rendering-styles/">more info</a>), and your because CSS has a dependency on a filtered image, the rendering is blocked until the dependency is satisfied.</p>
<p>What if you have several AlphaImageLoader filters on the page? They are processed synchronously one after the other so the problem is multiplied. If you have 5 images, each delayed 2 seconds on the server, then the browser freezes for a total of 10 seconds.</p>
<h3>Time and Memory &mdash; Side Effects #2 and #3</h3>
<p>Another negative effect of using the AlphaImageLoader is the increase of the amount of memory required to process and apply the filters. These days we might be tempted to think our visitors&#8217; computers have a virtually indefinite supply of memory, but for older computers (those more likely to run IE6 and under) this may not be the case.</p>
<p>And at the end of it, it&#8217;s the performance we&#8217;re most interested in, performance as measured by the time it takes for the page to load in the browser. Let&#8217;s do a test to measure how much time and memory is required by the filters.</p>
<p>First, let&#8217;s have a baseline page &mdash; one that has a hundred &lt;div&gt;s with the same non-filtered background image. Then let&#8217;s have a second page with a filter applied to the divs (all 100 divs use the same). A hundred elements with filtered backgrounds is unlikely to be found in a normal page, but a little exaggeration will help with the measurements.</p>
<p>The time is measured from the start of the page to the onload event of the page, after the images have been cached, thus eliminating the time required to download the page and the images. The memory consumption is measured with the help of the <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">ProcessExplorer</a> tool and given as the before/after delta of the private bytes measurement, showing the &#8220;price&#8221; of rendering the page.
<p>Here are the median results from 10 runs in IE6 on a PC with a dual 2GHz CPU and 500M RAM. On a less powerful computer, the load times are likely to be even worse.</p>
<style type="text/css">td.number{text-align:right}</style>
<table border="1">
<caption>AlphaImageLoader test results</caption>
<tbody>
<tr>
<th>test page</th>
<th>time, seconds</th>
<th>memory, MB</th>
</tr>
<tr>
<td><a href="alpha-no-filter-100.html">Test #1 &#8211; no filters</a></td>
<td class="number">0.031</td>
<td class="number">0.6</td>
</tr>
<tr>
<td><a href="alpha-filter-100.html">Test #2 &#8211; with filters</a></td>
<td class="number">0.844</td>
<td class="number">46.8</td>
</tr>
</tbody>
</table>
<p>As you can see, the AlphaImageLoader effect is pretty bad &mdash; our test page loads 27 times slower and eats up 78 times more memory. These results are, of course, highly speculative &mdash; it&#8217;s just one image tested on just one PC (relatively powerful and underworked). With different images, applied to a different number of elements and on different machines, results may vary considerably, especially when there&#8217;s less RAM or CPU, or if you throw network latency (side effect #1) into the mix. But this example illustrates the important concepts:</p>
<ul>
<li>AlphaImageLoader is slow and requires more memory</li>
<li>It&#8217;s applied per element, not per image</li>
</ul>
<p>If you have a sprite image and you use it for different elements (sprites with alpha filters are trickier, <a href="http://www.julienlecomte.net/blog/2007/07/4/">but doable</a>), you&#8217;ll pay the penalty for each element the sprite is used on.</p>
<h3>Yahoo! Search Case Study</h3>
<p>Using lab tests like the one above can give us some idea of the AlphaImageLoader &#8220;price,&#8221; and you might be tempted to test and calculate approximately how much you pay for each filtered element, but there&#8217;s nothing better than a real life test with millions of requests coming form different parts of the world with different browsers, computers and bandwidth.</p>
<p><a href="http://search.yahoo.com/search?p=yahoo+search">Yahoo!&#8217;s search results page</a> used to have a truecolor PNG sprite and employed AlphaImageLoader to achieve the transparency (an <a href="http://us.js2.yimg.com/us.yimg.com/i/us/sch/el/ngsprt_srp_20071130.png">older version of the sprite</a> is still around if you&#8217;re curious). Replacing the truecolor PNG with a gracefully degrading PNG8 (<a href="http://yuiblog.com/blog/2008/11/04/imageopt-2/">discussed previously</a>) decreased the pageload time by 50-100ms for the users of IE 5 and 6. 100ms may not seem like much, but for a page that loads under a second, it&#8217;s at least 10%. Also, according to <a href="http://home.blarg.net/%7Eglinden/StanfordDataMining.2006-11-29.ppt">an Amazon study</a>, 100ms slower means 1% fewer sales (even for their content-heavy pages). Earning 1% more by just replacing an image doesn&#8217;t look like a bad deal at all.</p>
<h3>So Now What?</h3>
<p>The best thing would be to avoid AlphaImageLoader completely and, like Y!Search, take the time to create PNG8 images that degrade nicely in IE6 and look good in all other browsers. How do you create a gracefully degrading PNG8? Well, create a GIF-like image first, one that has only fully transparent or fully opaque pixels. After making sure it looks acceptable (it will look like this in IE6), proceed to enhancing the image with semi-transparent pixels which will smooth any rounded corners or other parts that would benefit from transparency. Unfortunately, as far as I know, Fireworks is currently the only image processing software capable of handling alpha transparency in PNG8. You can also try command line tools such as <a href="pngnq.sourceforge.net">pngnq</a> and <a href="http://www.libpng.org/pub/png/apps/pngquant.html">pngquant</a>, although automated truecolor-to-palette PNG conversion might not always yield satisfactory results and you might need to pick the fully opaque pixels manually.</p>
<p>There might be cases when you won&#8217;t be able to get by with a PNG8 and absolutely need to use AlphaImageLoader &mdash; for example when most or all pixels are semi-transparent (imagine a &#8220;play&#8221; button over a video thumbnail). Dave Artz of AOL has some <a href="http://www.artzstudio.com/2008/07/png-alpha-transparency-no-clear-winner/">other cases</a> where PNG8 will not be good enough. In such cases (but only after you try your best to persuade the designer to reconsider the use of transparency), make sure you use the underscore hack (<code>_filter</code>) so that you don&#8217;t penalize IE7 users.</p>
<p>Sometimes instead of PNG8 people use GIF for IE6 and truecolor PNG for the others, but that&#8217;s not necessary; with one PNG8 you achieve both binary and alpha transparency.</p>
<p>Additional benefits from using a PNG8 are:</p>
<ol>
<li>PNG8 is usually smaller than truecolor PNG,</li>
<li>only one image to maintain for all browsers</li>
<li>cleaner CSS with no hacks, branches or proprietary tags</li>
<li>ability to repeat background</li>
</ol>
<h3>Transparency with VML</h3>
<p>Using VML is yet another option in IE to make a truecolor PNG transparent, and it solves several problems: alpha transparency, performance, and background repeat. Unfortunatelly, it comes with the price of extra non-standard markup (or dependency on JavaScript to generate it if you want your initial markup clean) and more propritary CSS. Here&#8217;s an example on how to implement it.</p>
<p>If, for example, you have an empty div, you need to wrap it in one VML <code>:rect</code> (or <code>:shape</code>) and one <code>:fill</code> element, like this:</p>
<pre>
&lt;v:rect&gt;
  &lt;v:fill type=&quot;tile&quot; src=&quot;alphatest.png&quot;&gt;
    &lt;div&gt;&amp;nbsp;&lt;/div&gt;
  &lt;/v:fill&gt;
&lt;/v:rect&gt;
</pre>
<p>Somewhere in the markup before that you also need to declare a VML namespace:</p>
<pre>
&lt;xml:namespace ns=&quot;urn:schemas-microsoft-com:vml&quot; prefix=&quot;v&quot; /&gt;
</pre>
<p>And in your stylesheet you need:</p>
<pre>
v\:rect  {
    behavior:url(#default#VML);
    width: 100px;
    height: 100px;
    display: block;
}

v\:fill  {
    behavior:url(#default#VML);
}
</pre>
<p><a href="http://yuiblog.com/assets/image-opt-tests/vml.html">A test page</a> with 100 VML <code>rect</code> elements loads in 0.094 seconds (almost 10 times faster than using filters) and the memory usage is under 4Mb (10 times less than the filtered page).</p>
<p>As you can see this solution adds more markup and proprietary CSS, but it&#8217;s still a solution and doesn&#8217;t have the penalties of the AlphaImageLoader.</p>
<p>(Thanks go to <a href="http://dillerdesign.com/experiment/DD_roundies/">this post</a> by Drew Diller and also <a href="http://www.htmlremix.com/curved-corner-border-radius-cross-browser/">HTML Remix</a>, who accidentally found this side effect while working on another problem  &mdash; rounded corners with VML, via <a href="http://snook.ca/archives/html_and_css/ie-rounded/">snook.ca</a>)</p>
<h3>P.S. &#8230;and What about Other Filters</h3>
<p>AlphaImageLoader is not the only filter that exists. Another popular one is the opacity filter. </p>
<p>For example, for 50% element opacity developers use the properties: </p>
<ul>
<li><code>opacity: 0.5</code> (standard),</li>
<li><code>-moz-opacity: 0.5</code> (early Mozilla versions, before Firefox 0.9), and</li>
<li>for IE, <code>filter: alpha(opacity=50)</code>.</li>
</ul>
<p>A quick test in IE6 shows that the opacity filter is not nearly as slow as the AlphaImageLoader, but it&#8217;s still making the page slower and takes the same amount of memory. This test uses color background, not an image, but even with an image the results are pretty much the same.</p>
<table border="1">
<caption>opacity filter test results</caption>
<tbody>
<tr>
<th>test page</th>
<th>time, seconds</th>
<th>memory, MB</th>
</tr>
<tr>
<td><a href="alpha-no-opacity-100.html">Test #3 &#8211; 100 divs, no opacity</a></td>
<td class="number">0.016</td>
<td class="number">0.2</td>
</tr>
<tr>
<td><a href="alpha-opacity-100.html">Test #4 &#8211; 100 divs with opacity</a></td>
<td class="number">0.093</td>
<td class="number">46.7</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/12/08/imageopt-5/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Image Optimization, Part 3: Four Steps to File Size Reduction</title>
		<link>http://www.yuiblog.com/blog/2008/11/14/imageopt-3/</link>
		<comments>http://www.yuiblog.com/blog/2008/11/14/imageopt-3/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 16:37:31 +0000</pubDate>
		<dc:creator>Stoyan Stefanov</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/11/14/imageopt-3/</guid>
		<description><![CDATA[

About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript.


This is part 3 in an ongoing series. You can read the other parts [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<div class="intro">
<p><img src="http://yuiblog.com/assets/stoyan.jpg" alt="Stoyan Stefanov." align="right" hspace="10" vspace="5"><em><strong>About the Author:</strong><a href="http://www.phpied.com"> Stoyan Stefanov</a> is a Yahoo! web developer working for the <a href="http://developer.yahoo.com/performance/">Exceptional Performance</a> team and leading the development of the <a href="http://developer.yahoo.com/yslow/">YSlow</a> performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">Object-Oriented JavaScript</a>.</em></p>
</div>
</div>
<p><em>This is part 3 in an ongoing series. You can read the other parts here:</em></p>
<ul>
<li><a href="http://yuiblog.com/blog/2008/10/29/imageopt-1/">Image Optimization Part 1: The Importance of Images</a></li>
<li><a href="http://yuiblog.com/blog/2008/11/04/imageopt-2/">Image Optimization Part 2: Selecting the Right File Format</a></li>
<li><a href="http://yuiblog.com/blog/2008/12/05/imageopt-4/">Image Optimization Part 4: Progressive JPEG…Hot or Not?</a></li>
</ul>
<p>This post is about some common tools you can use to reduce the file size of your images. The idea is to be able to just take the images your designer has created and instead of using them &#8220;as is&#8221;, go ahead and tidy them up in short time and no effort, without even looking at them.</p>
<p>The good news is that this process is:</p>
<ul>
<li><em>lossless</em> &#8211; you strip bytes, hence you lose some information, but not the pixel data and the resulting image looks exactly the same as the source with no quality loss</li>
<li><em>uses free tools</em> &#8211; all the tools we mention here are free and open-source, and work on both Windows and Unix</li>
<li><em>automated</em> &#8211; since these are command line tools, they are easy to script and automate; one example of such automation is the <a href="http://smush.it">smush.it</a> tool</li>
</ul>
<h3>Step 1: Crush PNG</h3>
<p>PNGs store information in so-called &#8220;chunks&#8221; and not all of those chunks are required for the display of the image. In fact most of them are not. You can safely use a tool such as <a href="http://pmt.sourceforge.net/pngcrush/">pngcrush</a> and strip all the unneeded chunks. For example:</p>
<pre>&gt; pngcrush -rem alla -brute -reduce src.png dest.png</pre>
<p>Let&#8217;s take a look at the options of this command:</p>
<ul>
<li><code>src.png</code> is the source image, <code>dest.png</code> is the destination (result) image</li>
<li><code>-rem alla</code> means remove all chunks but keeps the one for transparency</li>
<li><code>-reduce</code> tries to reduce the number of colors in the palette if this is possible</li>
<li><code>-brute</code> tries over a hundred different methods for optimization in addition to the default 10. It&#8217;s slower and most of the times doesn&#8217;t improve much. But if you&#8217;re doing this process &#8220;offline&#8221;, one or two more seconds are not important since there&#8217;s a chance if a filesize win. Remove this option in performance-sensitive scenarios.</li>
</ul>
<p>Running this command on the PNGs found on Alexa&#8217;s top 10 sites gives us an average file size reduction of <strong>16.05%</strong>. This means you can easily <strong>strip weight off your PNG images</strong>, save bandwidth and disk space and improve load times, without sacrificing quality and without even touching a single line of application code.</p>
<p>PNGcrush is only one of the available tools for this sort of optimizations. Other tools you can take a look at include:
<ul>
<li><a href="http://www.pobox.com/~jason1/pngrewrite/">pngrewrite</a></li>
<li><a href="http://www.cs.toronto.edu/~cosmin/pngtech/optipng/">OptiPNG</a></li>
<li><a href="http://advsys.net/ken/utils.htm">PNGOut</a></li>
</ul>
<p>Now that we&#8217;ve got a pretty good PNG solution, let&#8217;s see if we can do the same for the other image types.</p>
<h3>Step 2: Strip JPG Metadata</h3>
<p>JPEGs files contain meta data such as:
<ul>
<li>comments</li>
<li>application-specific (think Photoshop) meta data</li>
<li>EXIF information such as camera information, date the photo was taken and even thumbnails of the actual image or audio!</li>
</ul>
<p>This meta data is not required for the display of the image and can safely be stripped with no pixel quality loss. As discussed previously, JPEG is a lossy format, which means you lose quality every time you save. But luckily there are some operations that are lossless. Such operations include cropping a part of the image, rotation and the personal favorite &#8211; copying metadata. One tool that allows you to do these is called <a href="http://jpegclub.org/">jpegtran</a>.</p>
<p>Here&#8217;s a command to copy the source image, optimize it and don&#8217;t carry over any metadata in the new copy:</p>
<pre>&gt; jpegtran -copy none -optimize src.jpg dest.jpg</pre>
<p>Note that depending on the version you have, you might need to use the syntax ending with <code>src.jpg &gt; dest.jpg</code></p>
<p>The <code>-optimize</code> option will cause jpegtran to optimize the Huffman tables and improve compression.</p>
<p>Running this command on Alexa top 10 sites resulted in average savings of <strong>11.85%</strong>.</p>
<p>You may be able to further improve image size by using jpegtran&#8217;s <code>-progressive</code> option. It produces JPEGs that load progressively in the browser, starting from a lower quality version of the image and improving as new image information arrives.</p>
<p><strong>Important note on stripping meta information: do it only for images that you own, because when jpegtan strips all the meta, it also strips any copyright information contained in the image file.</strong></p>
<h3>Step 3: GIF to PNG</h3>
<p>What&#8217;s the best way to improve a GIF? Convert it to a PNG. As funny as it may sound, it&#8217;s true. Most of the time you get a smaller file size from a PNG and the same quality and browser support, <a href="http://yuiblog.com/blog/2008/11/04/imageopt-2/">as we discussed in a previous article</a>. Note that PNG will not always be smaller, but most of the time it will be, so it&#8217;s worth checking after the conversion and keeping the smaller of the two files.</p>
<p>In order to automatically change your GIFs, you can use ImageMagick&#8217;s <code>convert</code>:</p>
<pre>&gt; convert image.gif image.png</pre>
<p>If you want to force PNG8 format you can use:</p>
<pre>&gt; convert image.gif PNG8:image.png</pre>
<p>This is probably not necessary, since GIFs will most likely be converted to a PNG8 anyway because ImageMagick picks the appropriate format based on the number of colors.</p>
<p>Once you&#8217;ve converted the GIF to a PNG, don&#8217;t forget to still crush the PNG result (as shown in step 1).</p>
<p>If the top 10 sites switch all their GIFs for PNGs (except those that don&#8217;t yield a smaller file size), on average, this will result in <strong>20.42%</strong> file size reduction. The only inconvenience here is that you also need to write a search/replace script to find all the references to the GIF files and change them to the new PNG versions.</p>
<h3>Step 4: Optimize GIF animations</h3>
<p>Now that all GIFs are PNGs, PNGs are crushed and so are the JPEGs, what do we have left? GIF animations. One tool that can help you with those guys is called <a href="http://www.lcdf.org/gifsicle/">GIFsicle</a>. Since the animations consist of frames and some parts of the image don&#8217;t change from one frame to another, GIFsicle doesn&#8217;t carry over the duplicate pixel information. The way to run it is:</p>
<pre>&gt; gifsicle -O2 src.gif &gt; dest.gif</pre>
<h3>Smush.it</h3>
<p>As we said at the beginning, the beauty of those four steps is that they don&#8217;t cause quality loss, so you don&#8217;t have to open and compare the results before and after. They are also all command-line tools that can be automated easily. So you have nothing to lose by running all your images through those tools before you FTP them to your web server, you can only win.</p>
<p>And you can always try the <a href="http://smush.it">smush.it</a> tool, just to get an idea of how much you can potentially save.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/11/14/imageopt-3/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Image Optimization Part 2: Selecting the Right File Format</title>
		<link>http://www.yuiblog.com/blog/2008/11/04/imageopt-2/</link>
		<comments>http://www.yuiblog.com/blog/2008/11/04/imageopt-2/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 17:16:42 +0000</pubDate>
		<dc:creator>Stoyan Stefanov</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/11/04/imageopt-2/</guid>
		<description><![CDATA[

About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript.


This is part 2 in an ongoing series. You can read the other parts [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<div class="intro">
<p><img src="http://yuiblog.com/assets/stoyan.jpg" alt="Stoyan Stefanov." align="right" hspace="10" vspace="5"><em><strong>About the Author:</strong><a href="http://www.phpied.com"> Stoyan Stefanov</a> is a Yahoo! web developer working for the <a href="http://developer.yahoo.com/performance/">Exceptional Performance</a> team and leading the development of the <a href="http://developer.yahoo.com/yslow/">YSlow</a> performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">Object-Oriented JavaScript</a>.</em></p>
</div>
</div>
<p><em>This is part 2 in an ongoing series. You can read the other parts here:</em></p>
<ul>
<li><a href="http://yuiblog.com/blog/2008/10/29/imageopt-1/">Image Optimization Part 1: The Importance of Images</a></li>
<li><a href="http://yuiblog.com/blog/2008/11/14/imageopt-3/">Image Optimization Part 3: Four Steps to File Size Reduction</a></li>
<li><a href="http://yuiblog.com/blog/2008/12/05/imageopt-4/">Image Optimization Part 4: Progressive JPEG…Hot or Not?</a></li>
</ul>
<p>This second installment of the image optimization series talks about file formats and how to chose the right one for the job. We&#8217;ll briefly discuss the popular GIF and JPEG formats and then move on to highlighting the rock star, PNG, hopefully helping correct some misconceptions about it.</p>
<h3>GIF</h3>
<p>GIF is a palette (also called &#8220;indexed&#8221;) type of image. It contains a palette of indexed colors, up to 256, and for every pixel of the image there is a corresponding color index. The format is no longer subject to patent issues, so you can create GIFs without the risk of going to jail. (For more on the history of the GIF format, <a href="http://www.cloanto.com/users/mcb/19950127giflzw.html">click here</a>.)</p>
<p>GIF is a <em>non-lossy</em> format, which means that when you modify the image and save it, you don&#8217;t lose quality.</p>
<p>GIF also support animations, which, in the dark Web 1.0 ages, resulted in a plethora of blinking &#8220;new&#8221; images, rotating @ signs, birds dropping &#8230; an email, and other annoyances. In the much more civilized Web 2.0 era, we still have &#8220;loading&#8230;&#8221; animations while we wait for the results of the next Ajax request to update the page, but there are also things like the good old shiny sparkles that people like to put in their social network profiles. Nevertheless, animation support is here if you need it.</p>
<p>GIF also supports transparency, which is a sort of boolean type of transparency. A pixel in a GIF image is either fully transparent or it&#8217;s fully opaque.</p>
<h3>JPEG</h3>
<p>JPEG doesn&#8217;t have the 256 colors restriction associated with GIFs; it can contain millions of colors and it has great compression. This makes it suitable for photos and, in fact, most cameras store photos in JPEG format. It&#8217;s a <em>lossy</em> format, meaning you lose quality with every edit, so it&#8217;s best to store the intermediate results in a different format if you plan to have many edits. There are, however, some operations that can be performed losslessly, such as cropping a part of the image, rotating it or modifying meta information, such as comments stored in the image file.</p>
<p>JPEG doesn&#8217;t support transparency.</p>
<h3>PNG</h3>
<p>PNGs is a <em>non-lossy</em> format that comes in several kinds, but for practical purposes, we can think of PNGs as being of two kinds:</p>
<ol>
<li>PNG8, and</li>
<li>truecolor PNGs.</li>
</ol>
<p>PNG8 is a palette image format, just like GIF, and 8 stands for 8 bits, or 2<sup>8</sup>, or 256, the number of palette entries. The terms &#8220;PNG8&#8243;, &#8220;palette PNG&#8221; and &#8220;indexed PNG&#8221; are used interchangeably. </p>
<p>How does PNG8 compare to GIF?</p>
<ul>
<li>Pros:
<ul>
<li>it usually yields a smaller file size</li>
<li>it supports alpha (variable) transparency</li>
</ul>
</li>
<li>Cons:
<ul>
<li>no animation support</li>
</ul>
</li>
</ul>
<p>The second type of PNGs, truecolor PNGs, can have millions of colors, like JPEG. You can also sometimes come across the names PNG24 or PNG32.</p>
<p>And how does truecolor PNG compare to JPEG? On the pros side, it&#8217;s non-lossy and supports alpha transparency, but on the cons side, the file size is bigger. This makes truecolor PNG an ideal format as an intermediate between several edits of a JPEG and also in cases where every pixel matters and the file size doesn&#8217;t matter much, such as taking screeenshots for a help manual or some printed material.</p>
<h3>Internet Explorer and PNG transparency</h3>
<p>We said that both PNG types support alpha transparency, but there are some browser eccentricities that affect both types and about which you should be aware.</p>
<p>With PNG8, whenever you have semi-transparent pixels they appear as fully transparent in IE (version 6 and lower). This is not ideal but it&#8217;s still useful and is the same behavior that you get from a GIF. So by using a PNG8, in the worst case (IE &lt; 7) you get the same user experience as with a GIF, while for other browsers (Firefox, Safari, Opera) you get a better experience. Below is an example that illustrates this, note how in IE6 the semi-transparent light around the bulb is missing (source: <a href="http://www.sitepoint.com/blogs/2007/09/18/png8-the-clear-winner/">SitePoint</a>):</p>
<p><img src="http://yuiblog.com/assets/png8-transparency.png" alt="PNG8 alpha transparency" /></p>
<p>For truecolor PNGs, the situation is a much less attractive compromise. All the semi transparent pixels appear grey in IE prior to version 7 (source: <a href="http://www.w3.org/Graphics/PNG/inline-alpha.html">W3C</a>).</p>
<p><img src="http://yuiblog.com/assets/png-transparency.png" alt="transparency in truecolor PNG" /></p>
<p>IE7 introduces proper native support for alpha transparency in both PNG8 and truecolor PNGs. For earlier IE versions you need to fix the truecolor PNG transparency issue using CSS and an AlphaImageLoader filter, which we&#8217;ll discuss in more details in a follow-up article. (Spoiler alert: <em>avoid AlphaImageLoader</em>.)</p>
<h3>&#8220;All we are saying is: Give PiNG a chance!&#8221;</h3>
<p>Although PNG8 should be the preferred of the PNGs, because it&#8217;s smaller in filesize and degrades well in early IEs without special CSS filters, there are still some use cases for truecolor PNGs:</p>
<ul>
<li><strong>When the 256 colors of the PNG8 are not enough, you may need a truecolor PNG.</strong> This is a case you should try to avoid. On one hand, if you have thousands and thousands of colors, this starts to look like a case where JPEG will be better suited and will give better compression. On the other hand, if the colors are around a thousand or so, you may try to convert this image to a PNG8 and see if it looks acceptable. Very often, the human eye is not sensitive enough to tell the difference between 200 and 1000 colors. That depends on the image, of course; often you can safely remove 1000 colors, but sometimes removing even 2 colors results in an unacceptable image. In any event, try your potential truecolor PNG candidate as PNG8 and as JPEG and see if you like the result in terms of quality and file size.</li>
<li><strong>When most of the image is semi-transparent.</strong> If only a small part of the image is semi-transparent, like around rounded corners, the GIF-like degradation of PNG8 is often OK. But if most of the image is translucent (think a PLAY button over a video thumbnail), you might not have a choice but to use the AlphaImageLoader hack.</li>
</ul>
<p>At the end, let&#8217;s summarize what was discussed in this article highlighting that:</p>
<ul>
<li>JPEG is the format for photos.</li>
<li>GIF is the format for animations.</li>
<li>PNG8 is the format for everything else &mdash; icons, buttons, backgrounds, graphs&#8230;you name it.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/11/04/imageopt-2/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Image Optimization Part 1: The Importance of Images</title>
		<link>http://www.yuiblog.com/blog/2008/10/29/imageopt-1/</link>
		<comments>http://www.yuiblog.com/blog/2008/10/29/imageopt-1/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 20:28:35 +0000</pubDate>
		<dc:creator>Stoyan Stefanov</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/10/29/imageopt-1/</guid>
		<description><![CDATA[

About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript.


This is part 1 in an ongoing series. You can read the other parts [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<div class="intro">
<p><img src="http://yuiblog.com/assets/stoyan.jpg" alt="Stoyan Stefanov." align="right" hspace="10" vspace="5"><em><strong>About the Author:</strong><a href="http://www.phpied.com"> Stoyan Stefanov</a> is a Yahoo! web developer working for the <a href="http://developer.yahoo.com/performance/">Exceptional Performance</a> team and leading the development of the <a href="http://developer.yahoo.com/yslow/">YSlow</a> performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">Object-Oriented JavaScript</a>.</em></p>
</div>
</div>
<p><em>This is part 1 in an ongoing series. You can read the other parts here:</em></p>
<ul>
<li><a href="http://yuiblog.com/blog/2008/11/04/imageopt-2/">Image Optimization Part 2: Selecting the Right File Format</a></li>
<li><a href="http://yuiblog.com/blog/2008/11/14/imageopt-3/">Image Optimization Part 3: Four Steps to File Size Reduction</a></li>
<li><a href="http://yuiblog.com/blog/2008/12/05/imageopt-4/">Image Optimization Part 4: Progressive JPEG…Hot or Not?</a></li>
</ul>
<p>This is the first in a series of posts about image optimization. In this series, I&#8217;ll explore how images affect web site performance and what can you do to your images in order to improve page loading times. (I won&#8217;t say how many posts in this series, so that I can claim later that I underpromised and overdelivered&#8230;).</p>
<p>When you think about improving page response time, one of the first obvious things to think about is the page weight. It&#8217;s obvious that, all things being equal, the heavier a page is the slower it will be. If we take this to the extreme, we can say that the fastest page you can possibly have is the blank page. Once you start adding stuff to the blank page, you&#8217;re only making it slower. </p>
<p>On a more serious note, it really is up to you how much content you want to put on a page, so let&#8217;s focus on what comes next. After you&#8217;ve settled on the content, it&#8217;s your job to make sure the content and components are as small as possible. Following our <a href="http://developer.yahoo.com/performance/rules.html">Yahoo! performance best practices</a>, you should make sure that all plain text components (HTML, XML, CSS, JavaScript&#8230;) are sent compressed over the wire and that you minify CSS and JavaScript. </p>
<p>But what about the images, how can you speed them up without sacrificing quality and looks? But first, does it really matter?</p>
<h3>How important are the images?</h3>
<p>Before we start, let&#8217;s see if we should even bother with images. Lately we&#8217;ve been witnessing the rise of rich internet applications with lots of JavaScript &mdash; by &#8220;lots&#8221; meaning sometimes 300K or more worth of JavaScript code. In other cases, especially in advertising, Flash seems to be the weapon of choice. So, on average, how much of the page weight is images. It&#8217;s easy to answer this question by just looking at <a href="http://www.alexa.com/site/ds/top_sites">Alexa&#8217;s top 10 websites</a> in the world (as of October 2008) and use <a href="http://developer.yahoo.com/yslow/">YSlow</a> to check what percent of the total page weight is images. The results are given below.</p>
<table border="1" id="imagekweight" width="350">
<caption>Percentage of page weight that goes to images, average 46.6%</caption>
<tbody>
<tr>
<td>1</td>
<td>Yahoo!</td>
<td>39%</td>
</tr>
<tr>
<td>2</td>
<td>Google</td>
<td>75%</td>
</tr>
<tr>
<td>3</td>
<td>YouTube</td>
<td>37%</td>
</tr>
<tr>
<td>4</td>
<td>Live.com</td>
<td>94%</td>
</tr>
<tr>
<td>5</td>
<td>Facebook</td>
<td>39%</td>
</tr>
<tr>
<td>6</td>
<td>MSN</td>
<td>59%</td>
</tr>
<tr>
<td>7</td>
<td>MySpace</td>
<td>36%</td>
</tr>
<tr>
<td>8</td>
<td>Wikipedia</td>
<td>34%</td>
</tr>
<tr>
<td>9</td>
<td>Blogger</td>
<td>28%</td>
</tr>
<tr>
<td>10</td>
<td>Yahoo! JP</td>
<td>25%</td>
</tr>
</tbody>
</table>
<style type="text/css">
	#chart
	{
		width: 530px;
		height: 350px;
	}</p>
<p>	.chart_title
	{
		display: block;
		font-size: 1.2em;
		font-weight: bold;
		margin-bottom: 0.4em;
	}
</style>
<div id="chart">
</div>
<p><!--Include YUI Loader: --><br />
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yuiloader/yuiloader-min.js"></script> </p>
<p><!--Use YUI Loader to bring in your other dependencies: --><br />
<script type="text/javascript"> 
// Instantiate and configure YUI Loader: 
(function() { </p>
<p>	var initChart = function() {</p>
<p>		YAHOO.widget.Chart.SWFURL = "http://yui.yahooapis.com/2.6.0/build//charts/assets/charts.swf";</p>
<p>		YAHOO.example.imageweight =
		[
			{ company: "Yahoo!", percent: 39},
			{ company: "Google", percent: 75},
			{ company: "YouTube", percent: 37},
			{ company: "Live", percent: 94},
			{ company: "Facebook", percent: 39},
			{ company: "MSN", percent: 59},
			{ company: "MySpace", percent: 36},
			{ company: "Wikip.", percent: 34},
			{ company: "Blogger", percent: 28},
			{ company: "Y! Japan", percent: 25}
		];</p>
<p>		var myDataSource = new YAHOO.util.DataSource( YAHOO.example.imageweight );
		myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		myDataSource.responseSchema =
		{
			fields: [ "company", "percent" ]
		};</p>
<p>		var seriesDef = 
		[
			{ displayName: "% Pageweight in Images", yField: "percent" },
		];</p>
<p>		var percentAxis = new YAHOO.widget.NumericAxis();
		percentAxis.minimum = 0;
		percentAxis.maximum = 100;</p>
<p>		var mychart = new YAHOO.widget.ColumnChart( "chart", myDataSource,
		{
			yAxis: percentAxis,
			series: seriesDef,
			xField: "company",
			yField: "percent",
			style:
			{
				padding: 20,
				font: {
					size:9,
					color:"666666"
					},
				legend:
				{
					display: "bottom",
					padding: 7,
					spacing: 5,
					font:
					{
						color:"#666666",
						size: 12
					}
				}
			}
		});</p>
<p>	}</p>
<p>    var loader = new YAHOO.util.YUILoader({  
        require: ["charts"], 
        loadOptional: false, 
        combine: true, 
        filter: "MIN", 
        allowRollup: true, 
        onSuccess: initChart 
    }); </p>
<p>// Load the files using the insert() method. 
loader.insert(); 
})(); 
</script> </p>
<p>On average, <strong>46.6%</strong> of the page weight for these popular sites consists of images, included either inline with <code>&lt;img&gt;</code> tags or via CSS stylesheets. Other studies show that this percent can be even higher, depending on the cross section of sites you examine. The exact number is not important, because every site is unique and different from the average; for example <a href="http://amazon.com/">Amazon&#8217;s</a> home page was made of 75% images at the time of the experiment.</p>
<p>This is a massive percentage and it tells us one thing: There&#8217;s huge potential to improve the performance of websites if we can improve the way we handle the image payload. By focusing on images you can make a difference and delight your site visitors with a faster and more pleasant experience.</p>
<h3>To be continued&#8230;</h3>
<p>Over the course of the following weeks, we&#8217;ll be publishing more about image optimization. The topics for discussion include:</p>
<ul>
<li>different image formats and how to pick the right one</li>
<li>ways to put your images on a diet without compromising quality</li>
<li>optimizing generated images</li>
<li>the effect of using <code>AlphaImageLoader</code></li>
<li>favicons</li>
<li>CSS sprites</li>
<li>serving images faster</li>
</ul>
<p>The series of posts will not require Photoshop or other designers&#8217; domain-specific knowledge, so it should be pretty easy for anyone to learn and apply these techniques. More to come soon&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/10/29/imageopt-1/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Non-blocking JavaScript Downloads</title>
		<link>http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/</link>
		<comments>http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 18:41:39 +0000</pubDate>
		<dc:creator>Stoyan Stefanov</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/07/22/non-blocking-scripts/</guid>
		<description><![CDATA[

About the Author: Stoyan Stefanov is a Yahoo! web developer working for the Exceptional Performance team and leading the development of the YSlow performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called Object-Oriented JavaScript.


External JavaScript files block downloads and hurt your page performance, but there is an [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<div class="intro">
<p><img src="http://yuiblog.com/assets/stoyan.jpg" alt="Stoyan Stefanov." align="right" hspace="10" vspace="5"><em><strong>About the Author:</strong><a href="http://www.phpied.com"> Stoyan Stefanov</a> is a Yahoo! web developer working for the <a href="http://developer.yahoo.com/performance/">Exceptional Performance</a> team and leading the development of the <a href="http://developer.yahoo.com/yslow/">YSlow</a> performance tool. He also an open-source contributor, conference speaker and technical writer: his latest book is called <a href="http://www.packtpub.com/object-oriented-javascript-applications-libraries/book">Object-Oriented JavaScript</a>.</em></p>
</div>
</div>
<p>External JavaScript files block downloads and hurt your page performance, but there is an easy way to work around this problem: use dynamic scripts tags and load scripts in parallel, improving the page loading speed and the user experience.</p>
<h2>The problem: scripts block downloads</h2>
<p>Let&#8217;s first take a look at what the problem is with the script downloads. The thing is that before fully downloading and parsing a script, the browser can&#8217;t tell what&#8217;s in it. It may contain <code>document.write()</code> calls which modify the DOM tree or it may even contain <code>location.href</code> and send the user to a whole new page. If that happens, any components downloaded from the previous page may never be needed. In order to avoid potentially useless downloads, browsers first download, parse and execute each script before moving on with the queue of other components waiting to be downloaded. As a result, any script on your page blocks the download process and that has a negative impact on your page loading speed.</p>
<p>Here&#8217;s how the timeline looks like when downloading a slow JavaScript file (exaggerated to take 1 second). The script download (the third row in the image) blocks the two-by-two parallel downloads of the images that follow after the script:</p>
<p><img src="http://yuiblog.com/assets/non-block-yui/1.png" alt="Timeline - Blocking behavior of JavaScript files" width="325" height="223" /></p>
<p><a href="http://yuiblog.com/assets/non-block-yui/before.html">Here&#8217;s the example</a> to test yourself.</p>
<h2>Problem 2: number of downloads per hostname</h2>
<p>Another thing to note in the timeline above is how the images following the script are downloaded two-by-two. This is because of the restriction of how many components can be downloaded in parallel. In IE &lt;= 7 and Firefox 2, it&#8217;s two components at a time (following the HTTP 1.1 specs), but both IE8 and FF3 increase the default to 6.</p>
<p>You can work around this limitation by using multiple domains to host your components, because the restriction is two components <em>per hostname</em>. For more information of this topic check the article &#8220;<a href="http://yuiblog.com/blog/2007/04/11/performance-research-part-4/">Maximizing Parallel Downloads in the Carpool Lane</a>&#8221; by Tenni Theurer.</p>
<p>The important thing to note is that <em>JavaScripts block downloads across all hostnames</em>. In fact, in the example timeline above, the script is hosted on a different domain than the images, but it still blocks them.</p>
<h2>Scripts at the bottom to improve user experience</h2>
<p>As <a href="http://developer.yahoo.com/performance/rules.html">Yahoo!&#8217;s Performance rules</a> advise, you should put the scripts at the bottom of the page, towards the closing <code>&lt;/body&gt;</code> tag. This doesn&#8217;t really make the page load faster (the script still has to load), but helps with the progressive rendering of the page. The user perception is that the page is faster when they can see a visual feedback that there is progress.</p>
<h2>Non-blocking scripts</h2>
<p>It turns out that there is an easy solution to the download blocking problem: include scripts dynamically via DOM methods. How do you do that? Simply create a new <code>&lt;script&gt;</code> element and append it to the <code>&lt;head&gt;</code>:</p>
<pre>
var js = document.createElement('script');
js.src = 'myscript.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(js);
</pre>
<p>Here&#8217;s the same test from above, modified to use the script node technique. Note that the third row in the image takes just as long to download, but the other resources on the page are loading simultaneously:</p>
<p><img src="http://yuiblog.com/assets/non-block-yui/2.png" alt="Non-blocking JavaScript timeline" width="325" height="206" /></p>
<p><a href="http://yuiblog.com/assets/non-block-yui/after.html">Test example</a></p>
<p>As you can see the script file no longer blocks the downloads and the browser starts fetching the other components in parallel. And the overall response time is cut in half.</p>
<h2>Dependencies</h2>
<p>A problem with including scripts dynamically would be satisfying the dependencies. Imagine you&#8217;re downloading 3 scripts and <code>three.js</code> requires a function from <code>one.js</code>. How do you make sure this works?</p>
<p>Well, the simplest thing is to have only one file, this way not only avoiding the problem, but also improving performance by minimizing the number of  HTTP requests (<a href="http://developer.yahoo.com/performance/rules.html#num_http">performance rule #1</a>).</p>
<p>If you do need several files though, you can attach a listener to the script&#8217;s <code>onload</code> event (this will work in Firefox) and the <code>onreadystatechange</code> event (this will work in IE). Here&#8217;s a <a href="http://www.phpied.com/javascript-include-ready-onload/">blog post</a> that shows you how to do this. To be fully cross-browser compliant, you can do something else instead: just include a variable at the bottom of every script, as to signal &#8220;I&#8217;m ready&#8221;. This variable may very well be an array with elements for every script already included.</p>
<h2>Using YUI Get utility</h2>
<p>The <a href="http://developer.yahoo.com/yui/get/">YUI Get Utility</a> makes it easy for you to use script includes. For example if you want to load 3 files, <code>one.js</code>, <code>two.js</code> and <code>three.js</code>, you can simply do:</p>
<pre>
var urls = ['one.js', 'two.js', 'three.js'];
YAHOO.util.Get.script(urls);
</pre>
<p>YUI Get also helps you with satisfying dependencies, by loading the scripts in order and also by letting you pass an <code>onSuccess</code> callback function which is executed when the last script is done loading. Similarly, you can pass an <code>onFailure</code> function to handle cases where scripts fail to load.</p>
<pre>
var myHandler = {
    onSuccess: function(){
        alert(':))');
    },
    onFailure: function(){
        alert(':((');
    }
};

var urls = ['1.js', '2.js', '3.js'];
YAHOO.util.Get.script(urls, myHandler);
</pre>
<p>Again, note that YUI Get will request the scripts in sequence, one after the other. This way you don&#8217;t download all the scripts in parallel, but still, the good part is that the scripts are not blocking the rest of the images and the other components on the page. <a href="http://developer.yahoo.com/yui/examples/get/get-script-basic.html">Here&#8217;s a good example and tutorial on using YUI Get to load scripts</a>.</p>
<p>YUI Get can also include stylesheets dynamically through the method<br />
<code>YAHOO.util.Get.css()</code> [<a href="http://developer.yahoo.com/yui/examples/get/get-css-basic.html">example</a>].</p>
<p>Which brings us to the next question:</p>
<h2>And what about stylesheets?</h2>
<p>Stylesheets don&#8217;t block downloads in IE, but they do in Firefox. Applying the same technique of dynamic inserts solves the problem. You can create dynamic link tags like this:</p>
<pre>
var h = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.href = 'mycss.css';
link.type = 'text/css';
link.rel = 'stylesheet';
h.appendChild(link);
</pre>
<p>This will improve the loading time in Firefox significantly, while not affecting the loading time in IE.</p>
<p>Another positive side effect of the dynamic stylesheets (in FF) is that it helps with the progressive rendering. Usually both browsers will wait and show blank screen until the very last piece of stylesheet information is downloaded, and only then they&#8217;ll start rendering. This behavior saves them the potential work of re-rendering when new stylesheet rules come down the wire. With dynamic <code>&lt;link&gt;</code>s this is not happening in Firefox, it will render without waiting for all the styles and then re-render once they arrive. IE will behave as usual and wait.</p>
<p>But before you go ahead and implement dynamic <code>&lt;link&gt;</code> tags, consider the violation of the rule of separation of concerns: your page <em>formatting</em> (CSS) will be dependent on <em>behavior</em> (JS). In addition, this problem is going to be addressed in future Firefox versions.</p>
<h2>Other ways?</h2>
<p>There are other ways to achieve the non-blocking scripts behavior, but they all have their drawbacks.</p>
<table border="1">
<tr>
<th>Method</th>
<th>Drawback</th>
</tr>
<tr>
<td>Using <code>defer</code> attribute of the <code>script</code> tag</td>
<td>IE-only, unreliable even there</td>
</tr>
<tr>
<td>Using <code>document.write()</code> to write a script tag</td>
<td>
<ol>
<li>Non-blocking behavior is in IE-only </li>
<li><code>document.write</code> is not a recommended coding practice</li>
</ol>
</td>
</tr>
<tr>
<td><code>XMLHttpRequest</code> to get the source then execute with <code>eval()</code>.</td>
<td>
<ol>
<li>&#8220;<code>eval()</code> is evil&#8221;</li>
<li>same-domain policy restriction</li>
</ol>
</td>
</tr>
<tr>
<td>XHR request to get the source, create a new script tag and set its content</td>
<td>
<ol>
<li>more complex</li>
<li>same-domain policy</li>
</ol>
</td>
</tr>
<tr>
<td>Load script in an iframe</td>
<td>
<ol>
<li>complex</li>
<li>iframe overhead</li>
<li>same-domain policy</li>
</ol>
</td>
</tr>
</table>
<h2>Future</h2>
<p>Safari and IE8 are already changing the way scripts are getting loaded. Their idea is to download the scripts in parallel, but execute them in the sequence they&#8217;re found on the page. It&#8217;s likely that one day this blocking problem will become negligible, because only a few users will be using IE7 or lower and FF3 or lower. Until then, a dynamic script tag is an easy way around the problem.</li>
<h2>Summary</h2>
<ul>
<li>Scripts block downloads in FF and IE browsers and this makes your pages load slower.</li>
<li>An easy solution is to use dynamic <code>&lt;script&gt;</code> tags and prevent blocking.</li>
<li><a href="http://developer.yahoo.com/yui/get/">YUI Get Utility</a> makes it easier to do script and style includes and manage dependencies.</li>
<li>You can use dynamic <code>&lt;link&gt;</code> tags too, but consider the separation of concerns first.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Performance Research, Part 6: Less is More — Serving Files Faster by Combining Them</title>
		<link>http://www.yuiblog.com/blog/2008/07/21/performance-research-part-6/</link>
		<comments>http://www.yuiblog.com/blog/2008/07/21/performance-research-part-6/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 16:03:15 +0000</pubDate>
		<dc:creator>Tenni Theurer</dc:creator>
				<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/07/21/performance-research-part-6/</guid>
		<description><![CDATA[

This article is the sixth in a series of YUIBlog articles describing experiments conducted to learn more about optimizing web page performance (Part 1, Part 2, Part 3, Part 4, Part 5).


In Performance Research Part 1, we discussed how reducing the number of HTTP requests has the biggest impact on improving the response time and [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<div class="intro">
<p><em>This article is the sixth in a series of YUIBlog articles describing experiments conducted to learn more about optimizing web page performance (<a href="http://yuiblog.com/blog/2006/11/28/performance-research-part-1/">Part 1</a>, <a href="http://yuiblog.com/blog/2007/01/04/performance-research-part-2/">Part 2</a>, <a href="http://yuiblog.com/blog/2007/03/01/performance-research-part-3/">Part 3</a>, <a href="http://yuiblog.com/blog/2007/04/11/performance-research-part-4/">Part 4</a>, <a href="http://yuiblog.com/blog/2008/02/06/iphone-cacheability/">Part 5</a>).</em></p>
</p></div>
</div>
<p>In <a href="http://yuiblog.com/blog/2006/11/28/performance-research-part-1/">Performance Research Part 1</a>, we discussed how reducing the number of HTTP requests has the biggest impact on improving the response time and is often the easiest performance improvement to make. One technique without having to simplify the page design is to combine multiple scripts into a single script, and similarly combine multiple stylesheets into a single stylesheet.</p>
<blockquote><p>Combining multiple files reduces the extra bytes from HTTP headers as well as potential transfer latency caused by TCP slow starts, packet losses, etc.</p></blockquote>
<p>Figure 1 shows a graphical view of how time is spent loading a page with six separate scripts. Notice that for every file, the browser makes a separate HTTP request to retrieve the file. The gaps between the scripts indicate the time the browser takes to parse and render each script. Figure 2 shows the how time is spent loading a page with the same six scripts combined into a single script.</p>
<div class="figure">
<h4><strong>Figure 1.</strong> Loading a page with six separate scripts</h4>
<p><img src="http://yuiblog.com/assets/six-separate-scripts.gif" alt="Figure 1. Loading a page with six separate scripts" width="487" height="114" id="six-separate-scripts"/></p>
</div>
<div class="figure">
<h4><strong>Figure 2.</strong> Loading a page with one combined script</h4>
<p><img src="http://yuiblog.com/assets/one-single-script.gif" alt="Figure 1. Loading a page with one combined script" width="484" height="63" longdesc="six-separate-scripts.gif" id="six-separate-scripts"/></p>
</div>
<p>Combining JavaScript and CSS files as part of the development process can be burdensome. It usually makes sense during development to organize the code into logical modules as separate files. Typically, combining those separate files before product release is either a manual process or part of a build process. Every time one of the individual files is changed, the larger file needs to be re-combined and re-pushed. The cost of this across an organization as large as Yahoo! is significant.</p>
<h3>Serve Files Faster using Combo Handler</h3>
<p>Combo Handler, built in collaboration by <a href="http://developer.yahoo.com/performance/">Yahoo!&#8217;s Exceptional Performance team</a> and the groups that support our CDN, is one solution to combine multiple files into a single, larger file.</p>
<p>Combo Handler provides a way to allow developers to maintain the logical organization of their code in separate files, while achieving the advantages of combining those into a single file as part of the final user experience. It alleviates the need for the time-consuming re-build and re-push processes. In addition, Combo Handler integrates seamlessly into a content delivery network, taking full advantage of the <a href="http://developer.yahoo.com/performance/rules.html#cdn">benefits of a CDN</a> while reducing the drawbacks of dynamically combining separate files.</p>
<p>We&#8217;ve been using this service across many Yahoo! properties for some time now to help improve end users&#8217; response times. Thanks to the YUI team, it is <a href="http://yuiblog.com/blog/2008/07/16/combohandler/">now available</a> to all of you that are using the Yahoo!-hosted YUI JavaScript files. (Note: Combo-handling of CSS files is not supported at this time.) <a href="http://developer.yahoo.com/yui/articles/hosting/#configure">Head over to the YUI Configurator</a> to generate combo-ready filepaths customized for your specific YUI implementation.</p>
<h3>Combo Handler Best Practices</h3>
<p>When using Combo Handler to combine files, pay special attention to the order in which the files are specified. Not only could there be file dependencies, browsers will only use the cached version of a file if the filename extracted from the URL is identical. For example, suppose the following smaller files (<code>dom.js</code> and <code>event.js</code>) are combined into a single larger file using Combo Handler:</p>
<pre>
   http://yui.yahooapis.com/combo?event.js&#038;dom.js
   http://yui.yahooapis.com/combo?dom.js&#038;event.js
</pre>
<p>In the example above, the browser will download and cache both files separately because the filenames are actually different.</p>
<p>Also, you may not always want to combine all files into one single file. Suppose you have one or more scripts that are shared across multiple pages in your site in addition to scripts that are only used on specific pages. By combining everything into one large file and using this file across your entire site, some pages will spend time downloading more than it really needs. Instead, take a look at different types of combinations. You might combine the scripts that are used in every page across your site into one script. Then for each page or group of pages, combine common scripts into another separate script.</p>
<h3>Yahoo! HotJobs Combines and Reduces Response Time by 8%!</h3>
<p>The <a href="http://developer.yahoo.com/performance/">Exceptional Performance</a> team ran an experiment with <a href="http://hotjobs.yahoo.com">Yahoo! HotJobs</a> to determine the response time savings our users would benefit from by combining multiple files into a single file. Two real user test buckets were created for this experiment. In one bucket, users visited a page with six JavaScript files left uncombined. In the second bucket, users visited the same page with the six JavaScript files combined into one single file. </p>
<blockquote><p>Combining six JavaScript files into one single JavaScript file improved performance by almost 8% on average for Yahoo! HotJobs&#8217; users on broadband bandwidth speeds and 5% for users on lan. No design or feature changes required!</p></blockquote>
<p>Keep in mind that the page we tested was already highly optimized for performance and had a <a href="http://developer.yahoo.com/yslow">YSlow</a> &#8220;A&#8221; grade. The response time savings depend on a number of factors including number of files combined, browser caching patterns, etc. This experiment supported our previous research, which indicated that reducing HTTP requests is an effective way to improve response times for our end users.</p>
<h3>Takeaways</h3>
<p>Improve response times by combining multiple JavaScript and CSS files. Yahoo!&#8217;s <a href="http://yuiblog.com/blog/2008/07/16/combohandler/">Combo Handler</a> Service is one solution that provides a way to make fewer HTTP requests for Yahoo!-hosted JavaScript files, and also leverages the benefits of a Content Delivery Network.</p>
<ul>
<li>Combine scripts and stylesheets to reduce HTTP requests.</li>
<li>Look at different types of file combinations.</li>
<li>Avoid users from having to download more than they really need.</li>
<li>Pay special attention to the order in which files are combined.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/07/21/performance-research-part-6/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Combo Handler Service Available for Yahoo-hosted JS</title>
		<link>http://www.yuiblog.com/blog/2008/07/16/combohandler/</link>
		<comments>http://www.yuiblog.com/blog/2008/07/16/combohandler/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 19:16:39 +0000</pubDate>
		<dc:creator>Eric Miraglia</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/07/16/combohandler/</guid>
		<description><![CDATA[We&#8217;ve been talking for a long time at Yahoo about the importance of minimizing HTTP requests to improve performance. One important technique for YUI users has long been to use the pre-built &#34;rollup&#34; files (like yahoo-dom-event.js, which combines the YUI Core in a single minified HTTP request) and to create custom rollups that aggregate all [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been talking for a long time at Yahoo about <a href="http://yuiblog.com/blog/2006/11/28/performance-research-part-1">the importance of minimizing HTTP requests to improve performance</a>. One important technique for YUI users has long been to use the pre-built &quot;rollup&quot; files (like <code><a href="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js">yahoo-dom-event.js</a></code>, which combines the YUI Core in a single minified HTTP request) and to create custom rollups that aggregate all of your YUI JS content in a single file.  You&#8217;ll notice that we do a lot of this on our core Yahoo properties.  For example, if you go to <a href="http://sports.yahoo.com/sc">check on the Tour de France on Yahoo! Sports</a>, you&#8217;ll find that numerous YUI components are aggregated with custom Sports-specific JS resources in a single HTTP request (<a href="http://l.yimg.com/img.sports.yahoo.com/static/versioned_asset/v3/minify/js/editorial/js/yui/yuiloader-beta-min_2.5.1.r1.4.js;editorial/js/yui/dom-min_2.5.1.r1.4.js;editorial/js/yui/event-min_2.5.1.r1.4.js;editorial/js/yui/connection-min_2.5.1.r1.4.js;editorial/js/yui/animation-min_2.5.1.r1.4.js;editorial/js/yui/json-min.r1.3.js;editorial/js/constants.r1.15.js;editorial/js/globalsearch.r1.3.js;editorial/js/sports.r1.16.js;editorial/js/tabs.r1.20.js;editorial/js/cookie.r1.3.js;editorial/js/home_modules.r1.4.js;editorial/js/ticker.r1.3.js;editorial/js/window.r1.16.js;editorial/js/scorethin.r1.15.js;editorial/js/manager.r1.3.js;editorial/js/carousel.r1.11.js;editorial/js/player_search.r1.3.js;editorial/js/countdown.r1.5.js;editorial/js/oly.r1.13.js;editorial/js/mlbtv.r1.5.js;editorial/js/flyout_test.r1.20.js;editorial/js/ult.r1.3.js">here&#8217;s the aggregate file</a>).</p>
<p>Thanks to the hard work of the <a href="http://developer.yahoo.com/performance/">Yahoo Exceptional Performance team</a> and the groups that support our CDN, we&#8217;re now able to offer ad-hoc file aggregation &mdash; &quot;combo handling&quot; &mdash; to file served from <code>yui.yahooapis.com</code>. So, a request for the full <a href="http://developer.yahoo.com/yui/examples/editor/cal_editor.html">YUI Rich Text Editor</a>, which previously looked like this&#8230;</p>
<pre>&lt;script type=&quot;text/javascript&quot;
   src=&quot;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
   src=&quot;http://yui.yahooapis.com/2.5.2/build/container/container_core-min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
   src=&quot;http://yui.yahooapis.com/2.5.2/build/menu/menu-min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
   src=&quot;http://yui.yahooapis.com/2.5.2/build/element/element-beta-min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
   src=&quot;http://yui.yahooapis.com/2.5.2/build/button/button-min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
   src=&quot;http://yui.yahooapis.com/2.5.2/build/editor/editor-beta-min.js&quot;&gt;&lt;/script&gt; </pre>
<p>&#8230;can now be written this way:</p>
<pre>&lt;script type=&quot;text/javascript&quot;
src=&quot;http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&amp;
2.5.2/build/container/container_core-min.js&amp;2.5.2/build/menu/menu-min.js&amp;
2.5.2/build/element/element-beta-min.js&amp;2.5.2/build/button/button-min.js&amp;
2.5.2/build/editor/editor-beta-min.js&quot;&gt;&lt;/script&gt;</pre>
<p>In one step, this eliminates five separate HTTP requests.</p>
<p><a href="http://developer.yahoo.com/yui/articles/hosting/#configure"><img align="right" src="http://yuiblog.com/assets/combo.png" alt="Combo handling is built into the YUI Configurator interface." hspace="10" vspace="5" /></a> <strong>A few notes regarding combo handling on <code>yui.yahooapis.com</code></strong>:</p>
<ul>
<li>If you&#8217;re using the <a href="http://developer.yahoo.com/yui/articles/hosting/#configure">YUI Configurator</a>, this option (&quot;Combine All JS Files&quot;) is enabled by default as long as you&#8217;re using the default base path.</li>
<li>Combo-handling of YUI CSS files is not supported at this time. </li>
<li>In an upcoming release, we&#8217;ll provide built-in combo-handling support in <a href="http://developer.yahoo.com/yui/yuiloader/">YUI Loader</a> and restructure filepaths in YUI&#8217;s CSS resources to make them combinable as well.</li>
<li>YUI Configurator will always output the current version of the library, but all YUI JS files from 2.2.0 onward are present on <code>yui.yahooapis.com</code> and can be combined using the same combo-handling syntax.</li>
</ul>
<p>We hope combo handling provides a easy performance win for those of you letting Yahoo serve your YUI files. Discussion of combo handling and all YUI issues takes place <a href="http://tech.groups.yahoo.com/group/ydn-javascript/">in our community forum</a> &mdash; please join us there and let us know how this works for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/07/16/combohandler/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Helping the YUI Compressor</title>
		<link>http://www.yuiblog.com/blog/2008/02/11/helping-the-yui-compressor/</link>
		<comments>http://www.yuiblog.com/blog/2008/02/11/helping-the-yui-compressor/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 14:01:10 +0000</pubDate>
		<dc:creator>Nicholas C. Zakas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://yuiblog.com/blog/2008/02/11/helping-the-yui-compressor/</guid>
		<description><![CDATA[
							

Nicholas Zakas joined Yahoo! in 2006. He is the author of Professional Ajax and Professional JavaScript for Web Developers. He&#8217;s a contributor to our Yahoo! Juku. His Maintainable JavaScript presentation is available on YUI Theater.

							
Julien’s YUI Compressor is an incredibly useful tool for decreasing the size of your JavaScript files. Since it uses Rhino to [...]]]></description>
			<content:encoded><![CDATA[<div class="interview">
<p>							<!-- optional introduction inset --></p>
<div class="intro">
<p><a href="http://www.nczonline.net/">Nicholas Zakas</a> joined Yahoo! in 2006. He is the author of <cite><a href="http://www.amazon.com/Professional-Ajax-2nd-Programmer/dp/0470109491">Professional Ajax</a></cite> and <cite><a href="http://www.amazon.com/Professional-JavaScript-Developers-Wrox-Guides/dp/0764579088/ref=sr_1_2/103-9749654-9772648?ie=UTF8&#038;s=books&#038;qid=1180057873&#038;sr=1-2">Professional JavaScript for Web Developers</a></cite>. He&#8217;s a contributor to our <a href="http://yuiblog.com/blog/2007/12/06/juku/">Yahoo! Juku</a>. His <a href="http://yuiblog.com/blog/2007/05/25/video-zakas/">Maintainable JavaScript</a> presentation is available on <a href="http://developer.yahoo.com/yui/theater/">YUI Theater</a>.</p>
</p></div>
<p>							<!-- / optional introduction inset --></p>
<p>Julien’s <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a> is an incredibly useful tool for decreasing the size of your JavaScript files. Since it uses <a href="http://www.mozilla.org/rhino/" title="Rhino: JavaScript for Java">Rhino</a> to parse your JavaScript code, it can perform all kinds of smart operations to save bytes in a completely safe way:</p>
<ul>
<li>Replacement of local variable names with shorter (one, two, or three character) variable names.</li>
<li>Replacement of bracket notation with dot notation where possible (i.e. <code>foo["bar"]</code> becomes <code>foo.bar</code>).</li>
<li>Replacement of quoted literal property names where possible (i.e. { <code>"foo":"bar"</code> } becomes { <code>foo:"bar"</code> } ).</li>
<li>Replacement of escaped quotes in strings (i.e. <code>'aaa\'bbb'</code> becomes <code>"aaa’bbb"</code>).</li>
</ul>
<p>Running your JavaScript code through YUI Compressor results in tremendous savings by default, but there are things you can do to increase the byte savings even further.</p>
<h3 id="use-constants-for-repeated-values">Use Constants for Repeated Values</h3>
<p>In my talk, <a href="http://yuiblog.com/blog/2007/05/25/video-zakas/">Maintainable JavaScript</a>, I talk about using constants (really, just variables that you have no intention of changing) to store repeating values. The idea is that your code is more maintainable because you have a single place to change a value instead of multiple places. As it turns out, this technique also helps YUI Compressor to remove more bytes. Consider the following function:</p>
<pre><code>function toggle(element){
    if (YAHOO.util.Dom.hasClass(element, "selected")){
        YAHOO.util.Dom.removeClass(element, "selected");
    } else {
        YAHOO.util.Dom.addClass(element, "selected");
    }
}</code></pre>
<p>This simple function is designed to toggle the “<var>selected</var>” class on a given element. If the element has the class, then it’s removed; if the element doesn’t have the class, it’s added. As a result, the string “<var>selected</var>” appears three times in the function. The function takes 212 bytes (including white space). When compressed, the resulting code is as follows:</p>
<pre><code>function toggle(A){if(YAHOO.util.Dom.hasClass(A,"selected")){YAHOO.util.Dom.removeClass(A,"selected")}else{YAHOO.util.Dom.addClass(A,"selected")}}
</code></pre>
<p>This code weighs in at 146 bytes (a savings of 30%), but you can see that the string “<var>selected</var>” still appears three times. Moving the repeated value into a variable makes the code more maintainable and allows YUI Compressor to remove extra space. Here’s the rewritten function:</p>
<pre><code>function toggle(element){
    var className = "selected";
    if (YAHOO.util.Dom.hasClass(element, className)){
        YAHOO.util.Dom.removeClass(element, className);
    } else {
        YAHOO.util.Dom.addClass(element, className);
    }
}</code></pre>
<p>This code is slightly larger than the original (241 bytes versus 212 bytes), but compresses down to the following:</p>
<pre><code>function toggle(A){var B="selected";if(YAHOO.util.Dom.hasClass(A,B)){YAHOO.util.Dom.removeClass(A,B)}else{YAHOO.util.Dom.addClass(A,B)}}
</code></pre>
<p>Note that this compressed code only has one instance of “<var>selected</var>”, resulting in a final byte size of 136 bytes, 10 bytes fewer than the previous version. The savings grow as the instances of the string increase, so if you have 20 places where “<var>selected</var>” was being used, you’d see even greater savings.</p>
<p>Replacing repeated values in your code can lead to greater incremental savings as the number of repeated values increases, as well. It is worthwhile to consider this approach not just for strings, but also for numbers (even Boolean values, if you so desire).</p>
<h3 id="store-local-references-to-objects-and-values">Store Local References to Objects/Values</h3>
<p>The YUI Compressor can’t perform variable replacement for either global variables or multi-level object references, so it’s better to store these in local variables. The previous example has three instances of <var>YAHOO.util.Dom</var> in the source code, and so the compressed version also has three instances. By storing <var>YAHOO.util.Dom</var> in a local variable, you can reduce the number of times that it appears in the compressed code. For example:</p>
<pre><code>function toggle(element){
    var className = "selected";
    var YUD = YAHOO.util.Dom;
    if (YUD.hasClass(element, className)){
        YUD.removeClass(element, className);
    } else {
        YUD.addClass(element, className);
    }
}</code></pre>
<p>This version of the function is 238 bytes, and when compressed, shows even greater savings than the previous versions of the function:</p>
<pre><code>
function toggle(A){var B="selected";var C=YAHOO.util.Dom;if(C.hasClass(A,B)){C.removeClass(A,B)}else{C.addClass(A,B)}}
</code></pre>
<p>The final weight for this version is 118 bytes, a savings of 28 bytes over the original compressed function and 120 bytes smaller from the uncompressed version. And this is just one function, imagine if you got the same savings for all functions in your script.</p>
<p>Keep in mind that this technique also applies to object properties, so if <var>className</var> were a member of an object, its value should be stored locally as well. For instance:</p>
<pre><code>function toggle(element){
    var YUD = YAHOO.util.Dom;
    if (YUD.hasClass(element, Constants.className)){
        YUD.removeClass(element, Constants.className);
    } else {
        YUD.addClass(element, Constants.className);
    }
}</code></pre>
<p>In this function, <var>Constants.className</var> contains the class to use. The variable <var>Constants</var> is global, so its name cannot be replaced. You could set up a reference to <var>Constants</var>, but that is inefficient because you’re only using one property of that object in the function, so set up a reference to <var>Constants.className</var> to save even more bytes:</p>
<pre><code>function toggle(element){
    var className = Constants.className
    var YUD = YAHOO.util.Dom;
    if (YUD.hasClass(element, className)){
        YUD.removeClass(element, className);
    } else {
        YUD.addClass(element, className);
    }
}</code></pre>
<h3 id="avoid-eval">Avoid <code>eval()</code></h3>
<p>By this point, you’ve been told that <code>eval()</code> is evil multiple times and by multiple people. YUI Compressor agrees. The nature of <code>eval()</code> is such that the code executed has access to the variables that are present in the scope in which <code>eval()</code> was called. Because of that, YUI Compressor can’t safely do variable name changing when <code>eval()</code> is present. For example:</p>
<pre><code>function doSomething(code){
    var msg = "hi";
    eval(code);
}

doSomething("alert(msg)");   //”hi”</code></pre>
<p>Even though the string that is being passed to <code>eval()</code> exists outside of the function in which <code>eval()</code> is called, it still has access to the local variables in that function. Since YUI Compressor can’t possibly know that the variable code contains a reference to a variable in the function, it doesn’t change the variable names in the <code>doSomething()</code> function, resulting in a less-than-optimal compression. Remember this: any time you use <code>eval()</code> in a function, that function’s variables cannot be renamed. The best approach is, as often said, to avoid <code>eval()</code> at all costs. If you absolutely must use <code>eval()</code> for some reason, try to isolate it away from other code so that the amount of variable renaming issues are minimal. For example:</p>
<pre><code>function myEval(code){
    return eval(code);
}

function doSomething(code){
    var msg = “hi”;
    var count= 10;

    myEval(code);
}</code></pre>
<p>In this code, the call to <code>eval()</code> is isolated away from the main body of the <code>doSomething()</code> function. Now, YUI Compressor is free to replace variables in <code>doSomething()</code>.</p>
<h3 id="avoid-width">Avoid <code>with</code></h3>
<p>The <code>with</code> statement is another that is often <a href="http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/">recommended to avoid in JavaScript</a>. For YUI Compressor, the reason is the same for <code>eval()</code>: just the presence of <code>with</code> in a function causes variable renaming to be skipped for the entire function. There is just no way to keep track of variables versus object properties in the context of a <code>with</code> statement, so YUI Compressor rightly leaves the code as-is to avoid breaking the functionality. The best advice here is to avoid using <code>with</code> altogether. If you follow the advice of <a href="#store-local-references-to-objects-and-values">storing local copies of objects/properties</a>, you should have no use for <code>with</code>.</p>
<h3 id="use-the-verbose-option">Use the Verbose Option</h3>
<p>YUI Compressor has a “verbose” option (activated by the <code>–v</code> command line switch) that can help in the identification of some of these issues as well as a few others. The verbose option prints out warnings to the console indicating things that are preventing the YUI Compressor from fully doing its job. It will, for instance, tell you that a function contains <code>eval()</code> or the <code>with</code> statement, and therefore cannot be properly compressed. It also does analysis of variables, telling you if a variable was never defined (in which case it becomes global and cannot have its name replaced), if a variable was defined and never used (which just wastes space), and if a variable has been declared multiple times (also a waste of space).</p>
<h3 id="conclusion">Conclusion</h3>
<p>When used alone, the <a href="http://developer.yahoo.com/yui/compressor">YUI Compressor</a> achieves an excellent compression rate of your JavaScript code. The greatest byte savings are achieved by taking full advantage of variable replacement. The hints presented here have the primary goal of ensuring the YUI Compressor can do variable replacement whenever possible. Using constants to represent repeated values not only aids in compression, but also aids in the maintainability of your code by limited the number of areas that must be updated to accommodate a change in the value. Using local variables for multi-level object references allows for greater compression through variable replacement as well as providing faster runtime performance (local variable access is faster than global variable access and object property lookup). Perhaps most important is to ensure that you don’t use <code>eval()</code> or <code>with</code> when they’re not necessary, as each causes variable replacement to be turned off in the containing function. The YUI Compressor does a lot for you, but it can’t do everything. You can help it out greatly by following these tips.</p>
<p><!-- content ends here --></p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.yuiblog.com/blog/2008/02/11/helping-the-yui-compressor/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
