<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>getiblog &#187; loading</title>
	<atom:link href="http://blog.getify.com/tag/loading/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.getify.com</link>
	<description>open-source, performance, javascript &#38; ui musings</description>
	<lastBuildDate>Mon, 23 Aug 2010 20:50:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>LABjs: why not just concat?</title>
		<link>http://blog.getify.com/2009/11/labjs-why-not-just-concat/</link>
		<comments>http://blog.getify.com/2009/11/labjs-why-not-just-concat/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 05:41:08 +0000</pubDate>
		<dc:creator>getify</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[UI Architecture]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[labjs]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[page]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://blog.getify.com/?p=97</guid>
		<description><![CDATA[Update
[8/12/2010: I've posted a performance benchmark test to try and gather some numbers from the crowd-at-large on how single large concat'd files perform compared to chunking that big file into a couple of smaller files to download in parallel. I encourage you to go right now and click each of the buttons a few times, [...]]]></description>
			<content:encoded><![CDATA[<h5>Update</h5>
<p>[<strong>8/12/2010:</strong> I've posted a performance benchmark test to try and gather some numbers from the crowd-at-large on how single large concat'd files perform compared to chunking that big file into a couple of smaller files to download in parallel. I encourage you to go right now and click each of the buttons a few times, and then spread the link on to others. <a href="http://test.getify.com/concat-benchmark/test-1.html" target="_blank">Concat Performance Benchmark Test #1</a>]</p>
<p>By far the most prevalent question circulating right now in the wake of <a<br />
href="http://labjs.com">LABjs</a>&#8216; <a href="http://blog.getify.com/2009/11/labjs-new-hotness-for-script-loading/">initial public launch</a> (IPL, I guess!?) is the understandable: &#8220;Why do I need a loader like LABjs for multiple files when I can just concat everything into one file?&#8221;</p>
<p>Fair enough, this is an important question to address. You shouldn&#8217;t just take my implied assumption at face value; we need to dig into this so you understand why I feel so strongly that LABjs offers something better than what you are probably currently doing on your sites.</p>
<p>The short answer is, <strong>BOTH</strong>! You should concat files together when possible, <em>and</em> you should load your file(s) with a loader like LABjs. If you only do one or the other, you have missed out on the bigger picture of page-load optimization.</p>
<h2>YSlow/PageSpeed only value reducing HTTP requests</h2>
<p>Before I get into specifics about why I think LABjs brings something new and beneficial to the table, let&#8217;s tackle what I consider to be an interesting but perhaps misleading &#8220;truth&#8221;. I am firmly in a minority camp in my skepticism that reduction of HTTP requests is <em>the only</em> answer to the question of how to load a site more efficiently &#8212; actually, to be more accurate, how to load a site&#8217;s <em>JavaScript</em> more efficiently.</p>
<p>I bet that you are recalling the YSlow rules and the tried-and-true practice that concat&#8217;ing all your JS files into one file for production is the surest way to speed up your page loads. And you&#8217;re probably doubting my credibility because I would question such a fundamental &#8220;truth&#8221; as HTTP request reduction.</p>
<p>It is true that, especially for the largest sites on the internet (millions of page-views per month and more), the sheer volume of HTTP requests from a typical page profile (dozens of images, multiple style sheets, half a dozen JS files, etc) quickly overloads internet connections, server load-balancers, the user&#8217;s device, and even the browser itself. And anything you can do to reduce that overload will go a long way toward helping the site load with acceptable performance.</p>
<p>Sites like Yahoo and Google have taught us the basic principles of HTTP request reduction as an effective way to cut down on the overall page loading delays. For instance, consider <a href="http://spriteme.com">Spriting to combine images</a>, CSS concatenation, and, yes, JavaScript concatenation. Some sites, like Digg, have even experimented with the extreme &#8212; inlining all page resources, even binary image data, into a single multi-part request! And just recently, we saw a <a href="http://limi.net/articles/resource-packages/">proposal for Resource Packaging</a> as <a href="http://blog.getify.com/2009/11/resource-packaging-silver-bullet/">another way</a> to combine multiple types of data into single responses for HTTP request reduction.</p>
<p>If you run a site that has well over 50 million page views per month (I sure don&#8217;t!), you probably would assert that this is the easiest and <em>most</em> effective page-load optimization. And I&#8217;d be hard-pressed to go up against the brightest and the best at the world&#8217;s largest web properties to suggest that it&#8217;s not true, at least at their page-view volume.</p>
<h2>What&#8217;s true is true. Period.</h2>
<p>Here&#8217;s what I will suggest: maybe, just maybe, what&#8217;s true for Google at a hundred-million page views <em>may not identically and exactly</em> be true for the vast majority of us running sites in the tens-of-thousands page view realm. The &#8220;big boys&#8221; are up in rarefied air, and from there they certainly have the request volume to test, re-test, and &#8220;prove&#8221; these various rules of optimization. But in the same way that water behaves in strange, almost contradictory ways at extremes of hot or cold temperature, maybe the <em>art</em> of optimizing page loads isn&#8217;t as simple as &#8220;1 http request is always better than 2.&#8221;</p>
<p>Even for small sites (like the one you&#8217;re currently reading!), it&#8217;s still vitally important to reduce page load time and optimize for faster page views. So, I&#8217;ll readily and eagerly admit that a <em>key strategy</em> in that effort is for all of us to work to reduce our HTTP request loads.</p>
<h2>OK, so you agree concat&#8217;ing is best?</h2>
<p>Not so fast. Like I said, this issue is more complex than I think most of us are ready to admit. Too easily, we run the YSlow plugin tests on our site, and when we reduce HTTP requests by concat&#8217;ing our JS files, because it tells us to, not only does our perceived page-load times decrease, but our YSlow grade goes up! Success! We&#8217;ve proved the hypothesis, there&#8217;s nothing more to say, right?</p>
<p><strong>I don&#8217;t think this in and of itself means you have achieved the pinnacle of page-load performance, you&#8217;ve simply taken the first of many steps toward nirvana.</strong></p>
<p>In my opinion, loading your JavaScript in an efficient and optimized way cannot be boiled down into a simple set of &#8220;rules&#8221; &#8212; you can take such &#8220;truths&#8221; as good places to start, but you still have to refine the strategies to fit your particular site and audience. Indeed, this will start to look more like an art than a science, if done correctly. </p>
<h2>I&#8217;m not so sure&#8230;</h2>
<p>Maybe by this point, you&#8217;re still not convinced there&#8217;s more to the story, and you&#8217;re probably starting to feel like LABjs doesn&#8217;t really have that much to offer you. </p>
<p>Before you go, let me just say this: even if you <strong>only</strong> ever have one JavaScript file on your site, because concat&#8217;ing is the way to go for you, and you think that by putting a single &lt;script> tag at the bottom of your page, you&#8217;ve achieved the ultimate JavaScript loading solution &#8212; <strong>there is still some benefit to using LABjs to load that one file.</strong></p>
<p>Why? A regular &lt;script> tag in the main HTML, even at the foot of your document, will always block the page&#8217;s loading completion, which means that users will still be &#8220;stuck&#8221; waiting for that file to complete before they can do anything with your page. Now, for some pages, you may really not want the users to do anything until the JavaScript fully processes everything, but most people agree that designing sites for progressive-enhancement is a positive thing.</p>
<p>So, if you can design your sites so that there&#8217;s something meaningful for the users to see or even <em>do</em> while they wait for the JS to kick in, they will probably be happier with your site than if you just make them wait. And that&#8217;s where LABjs comes in. Loading JavaScript dynamically <strong>unblocks</strong> the rest of the page&#8217;s resources (and <a href="http://blog.getify.com/2009/11/why-dom-ready-still-sucks/">DOM-ready</a>!), freeing the user from the iron grip of the hanging &#8220;almost loaded but not yet&#8221; feeling that most sites force upon their users.</p>
<p>Include the small 4k of LABjs (via an <strong>inline</strong> &lt;script> tag) into your page, and use it to load your single JavaScript file, and I believe your users will see an improved, even slightly, user-experience on page-load, as long <a href="http://blog.getify.com/2009/11/labjs-new-hotness-for-script-loading/#ux-labjs">as you&#8217;re careful</a> about it.</p>
<h2>OK, so maybe there is more to this loading thing</h2>
<p>Keep reading if you&#8217;re ready to take the <a href="http://en.wikipedia.org/wiki/Red_pill">Red Pill</a> and dive deeper down the rabbit hole.</p>
<p><strong>(&#8230; more &#8230;)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.getify.com/2009/11/labjs-why-not-just-concat/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Why DOM-ready still sucks</title>
		<link>http://blog.getify.com/2009/11/why-dom-ready-still-sucks/</link>
		<comments>http://blog.getify.com/2009/11/why-dom-ready-still-sucks/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 16:32:44 +0000</pubDate>
		<dc:creator>getify</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[dom-ready]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[loading]]></category>

		<guid isPermaLink="false">http://blog.getify.com/?p=44</guid>
		<description><![CDATA[For those of you who are familiar with JavaScript libraries, you are probably at least a little bit aware that there is an &#8220;event&#8221; that occurs during the loading of a page which abstractly represents the point in time in which the browser has fully processed the structure (aka, the &#8220;DOM&#8221;) of the HTML document [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you who are familiar with JavaScript libraries, you are probably at least a little bit aware that there is an &#8220;event&#8221; that occurs during the loading of a page which abstractly represents the point in time in which the browser has fully processed the <strong>structure</strong> (aka, the &#8220;DOM&#8221;) of the HTML document being displayed. </p>
<p>This &#8220;event&#8221; is separate from (and ostensibly noticeably earlier than) the more familiar &#8220;onload&#8221; event, whose defined behavior means that all of the page&#8217;s initial resources have fully loaded &#8212; <strong>before</strong> &#8220;onload&#8221;, there is an earlier state at which things (such as images and styles) are still loading, but the basic plan or skeleton of the page is now known and understood and the browser knows what to expect.</p>
<p>This &#8220;event&#8221; is, in some browsers, an actual <em>event</em>, in other browsers it&#8217;s a state which can be passively observed, and in some browsers, it has to be divined by examining the alignment of the stars and taking into account the wind speed and gravitational pull of various heavenly bodies, divided by the&#8230;</p>
<p>You get the point, there&#8217;s an &#8220;event&#8221; that the browsers try (and some do better than others at this attempt) to expose for when the DOM is ready, which I will now conveniently and uncreatively call &#8220;DOM-ready&#8221;.</p>
<h2>So what if the DOM is ready or not?</h2>
<p>Let me make sure that you understand the importance of DOM-ready. In some cases, if you have JavaScript logic that tries to modify the DOM of your page <em>before</em> this DOM-ready event has occured/passed, you can wreak havoc with that browser, and in some cases, cause a fatal error and browser crash. </p>
<p>Think of it this way: if i&#8217;m standing on one foot, precariously balanced, and trying to pull on a sock over my bare foot, and my wife comes along and tugs on the rug I&#8217;m standing on, am I likely to get that sock put on successfully? Am I even likely to stay standing? Probably not.</p>
<p>So, it&#8217;s wise to make sure that you don&#8217;t take any actions which could adversely affect the browser&#8217;s ability to get his sock on, at least if you don&#8217;t want him to fall over. And that&#8217;s why we have the concept of DOM-ready &#8212; to make sure that you don&#8217;t manipulate the DOM too early.</p>
<p>Moreover, the major JS frameworks (for that matter, most of even the minor JS tools out there) all provide ways for you queue up code to be executed at some later time, that time being when the framework is able to detect that the event has occured and that it&#8217;s now safe for your code to go about the business of changing things.</p>
<h2>The frameworks all take care of that mess for me, right?</h2>
<p>Ahh, don&#8217;t we wish that were completely true. If it were, this post would be somewhat pointless, and you&#8217;d instead be enjoying eating a pizza or watching re-runs of Grey&#8217;s Anatomy.</p>
<p>Alas, the world is not that simple. There&#8217;s a little known, but I believe important, problem with the way DOM-ready detection is accomplished by the frameworks in a cross-browser way. I&#8217;m not going to bore you with all the details of just how this detection occurs, but let&#8217;s just say it gets a little hackish in some cases.</p>
<p>For the lion-share of use cases, the DOM-ready detection that the current framework versions employ is acceptable, at least for all the browsers that most of us really care about working correctly. If you&#8217;re still worried about Netscape 3.0 though, you&#8217;re on your own&#8230; good luck!</p>
<p>But, a subtle problem creeps in when you step outside the normal set of uses cases &#8212; those being where you simply include a &lt;script> tag at the top (or bottom, if you&#8217;re really clever) of your HTML document that loads your framework of choice (let&#8217;s say &#8220;jquery.js&#8221; for the purposes of this post). </p>
<p>Let&#8217;s say you have a mashup where you are pulling together multiple websites, and maybe <a href="http://jquery.com">jQuery</a> is not guaranteed to already be present on the page. &#8220;No problem!&#8221; you say, because you know how to load a script file on-demand, so you&#8217;ll just fire off a request to do so, and wait for jQuery to be present before you proceed to make calls against it (and, of course, then use it to change the page!)</p>
<p>Or, let&#8217;s say you have a bookmarklet that people can use to popup some nifty little widget in their page of choice, and again, you want to make sure that jQuery is present for your code to utilize. Again, we&#8217;ll just load jQuery on-the-fly, and we should be good to go, right?</p>
<p>Or even better, let&#8217;s just say you&#8217;ve got a really complex application, which loads different scripts into the page at different times depending on the state of what the user does. Yet again, you may need to fetch jQuery at some point well after the initial page loading occured.</p>
<p>There&#8217;s yet another use-case, which is actually the one that I ran into that caused me to discover this issue several months ago. If your page uses a script loader, such as <a href="http://labjs.com">LABjs</a>, the one I wrote in cooperation with <a href="http://stevesouders.com">Steve Souders</a> (of Yahoo! and Google fame), you have to be aware of how certain scripts, notably the frameworks, do their DOM-ready detection and how this applies when the script is loaded dynamically. </p>
<p>Because script-loaders remove the &#8220;blocking&#8221; nature of script loading that normal &lt;script> tags provide, it is actually quite possible to get into a situation where you load a script, like &#8220;jquery.js&#8221;, but the rest of the page is so well optimized that the DOM-ready event passes before jQuery arrives. You may very well not intend this to occur, but it can, and worse yet, it can happen as a race-condition, where it happens sometimes, and not others.</p>
<p><strong>(&#8230; more &#8230;)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.getify.com/2009/11/why-dom-ready-still-sucks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Loading JavaScript: Even a caveman can do it</title>
		<link>http://blog.getify.com/2009/11/loading-javascript-even-a-caveman-can-do-it/</link>
		<comments>http://blog.getify.com/2009/11/loading-javascript-even-a-caveman-can-do-it/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 06:46:44 +0000</pubDate>
		<dc:creator>getify</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[UI Architecture]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[minification]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://blog.getify.com/?p=8</guid>
		<description><![CDATA[I recently gave a talk about JavaScript Loading at JSConf.EU in Berlin. Video/audio of the talk will be made available online eventually, and I will link to it here once that happens. But, in fact, I will be re-presenting this same talk at the November meeting of Austin.Javascript (@AustinJS), on Tues Nov 17th, at 7:30pm. [...]]]></description>
			<content:encoded><![CDATA[<p>I recently gave a <a href="http://www.slideshare.net/shadedecho/loading-javascript-even-a-caveman-can-do-it">talk about JavaScript Loading</a> at <a href="http://jsconf.eu">JSConf.EU</a> in Berlin. Video/audio of the talk will be made available online eventually, and I will link to it here once that happens. But, in fact, I will be re-presenting this same talk at the November meeting of Austin.Javascript (<a href="http://twitter.com/austinjs">@AustinJS</a>), on <a href="http://www.linkedin.com/groupAnswers?viewQuestionAndAnswers=&#038;gid=1913658&#038;discussionID=9818762">Tues Nov 17th, at 7:30pm</a>. Come join us if you can! My hope is to push the envelope of how we all approach loading JavaScript resources into our pages.</p>
<p>The talk covers a number of different strategies, each of which I will be covering in subsequent posts here. But I wanted to give a quick overview of what you can expect:</p>
<ol>
<li><strong>Build-time optimization:</strong> it&#8217;s important to utilize build-process optimizations to manage local scripts and dependency chains. Depending on your server architecture and frameworks, there are lots of different choices that you have here, but the key is have a system that can manage this intelligence for you, freeing you and the other developers on your team from having to maintain the techniques yourselves.</li>
<li><strong>Load/run-time profiling:</strong> We&#8217;ve all heard the advice that the best way to get JavaScript resources onto the page is to only have one JavaScript file, meaning we should always concat all our individual files into one massive output file as it goes to the browser in production. But does this always make sense? I believe it only rarely does.
<p>There are a number of tools for profiling your site/application at load-time and at run-time, to decide what parts of your JavaScript are critical to load at the beginning, and what parts can be safely deferred until later and be lazy-loaded. Doing so not only speeds up the load time user-experience of your site/application, but it also often helps create better long(er)-term user-experience by more effectively utilizing the user&#8217;s browser cache. You can profile manually, or automatically, but you shouldn&#8217;t ever put a site or application into production without at least examining these strategies and trying a few different options.</li>
<li><strong>Post-optimize your minified JavaScript:</strong> For a long time, it felt like the book was closed on minification/obfuscation. There&#8217;s only so much white-space you can remove from a JavaScript file, and only so many variable names you can shorten, before a script seems as small as it&#8217;s going to get. But, it would seem there are more chapters to be written, yet.
<p>Recent releases of projects like Google Closure prove that there&#8217;s a lot more ground to cover on what we can do to optimize and shorten our scripts. I also have a project called <a href="http://mincir.it">Mincir</a> which is aimed at specifically running post-minification optimization steps on files to squeeze out more bytes. Smaller files means quicker loading and better user-experience. And that means happier users and happier developers.</li>
<li><strong>Dynamic, on-demand, parallel loading of JavaScript:</strong> While the landscape of how the browsers handle the &lt;script> tag is quite diverse and often ugly, there are ways to normalize this behavior cross-browser, and in the process allow all your JavaScript resources to load at the same time, in parallel with each other and the rest of the page assets. This is actually not that hard. But ensuring proper execution order, not only of loaded scripts, but also of coupled inline script executions, is where the magic happens.
<p>Luckily, a project of mine, called <a href="http://labjs.com">LABjs</a>, does just such a thing. It&#8217;s a general purpose, flexible, and capable JavaScript loader, that allows you to load just about any script or group of scripts onto any page, at any time.</li>
<li><strong>Load and cache &#8220;not-code&#8221;, execute &#8220;code&#8221; later:</strong> Sometimes, the best way to get JavaScript onto the page is not to load JavaScript at all. This may seem like a crazy statement, but you actually have some options as to how your &#8220;code&#8221; gets delivered to the page. For instance, you can transfer the code inside a multi-line comment block, or even as an escaped string literal assigned to a JavaScript variable. You may ask, why would I do such a crazy thing? Well, it turns out that there&#8217;s a significant amount of time spent processing JavaScript once it&#8217;s loaded onto a page.
<p>So, if on one page you want to &#8220;preload&#8221; a script into the cache for use on a subsequent page, like you have done with images for years, you pay a penalty for that script simply being added to your page. Unless of course it&#8217;s not really a script at that point. If it&#8217;s a comment or a string literal, then it is loaded and saved into the browser cache, but there&#8217;s almost no processing time on the page at that point. Later, on another page, you can recall this &#8220;not-code&#8221; from cache, and transform it into executable code. </p>
<p>This type of technique is quite valid for desktop browsers, but is particularly helpful for mobile browsers where the biggest bottleneck is not actually usually the  connection speed but the processing power of the mobile device in handling large amounts of not-used-yet code while rendering a page.</li>
</ol>
<p>None of these techniques are in and of themselves a complete solution to this issue, nor are these ideas the final word on the topic. But it&#8217;s time we evolve how we load our JavaScript onto pages and move beyond the 1990&#8217;s version of the plain ol&#8217; &lt;script> tag. I hope this talk and these posts will get that ball rolling.</p>
</p>
<div style="width:425px;text-align:left" id="__ss_2452641">
<div><a href="http://www.slideshare.net/shadedecho/loading-javascript-even-a-caveman-can-do-it" title="Loading JavaScript: Even a caveman can do it">Loading JavaScript: Even a caveman can do it</a></div>
</p>
<p><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=caveman-091108165033-phpapp02&#038;stripped_title=loading-javascript-even-a-caveman-can-do-it" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><param name="wmode" value="opaque"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=caveman-091108165033-phpapp02&#038;stripped_title=loading-javascript-even-a-caveman-can-do-it" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355" wmode="opaque"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/shadedecho">Kyle Simpson</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.getify.com/2009/11/loading-javascript-even-a-caveman-can-do-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Content Delivery Network via getiblog.2static.it

Served from: blog.getify.com @ 2010-09-07 21:17:54 -->