Upload
pearl-brock
View
17
Download
0
Embed Size (px)
DESCRIPTION
Even Faster Web Sites. http://stevesouders.com/docs/jquery-20090913.ppt Disclaimer: This content does not necessarily reflect the opinions of my employer. the importance of frontend performance. 9%. 91%. 17%. 83%. iGoogle, primed cache. iGoogle, empty cache. Make fewer HTTP requests - PowerPoint PPT Presentation
Citation preview
17%
83%
iGoogle, primed cache
the importance of frontend performance
9% 91%
iGoogle, empty cache
14 RULES
1. MAKE FEWER HTTP REQUESTS
2. USE A CDN3. ADD AN EXPIRES HEADER4. GZIP COMPONENTS5. PUT STYLESHEETS AT THE
TOP6. PUT SCRIPTS AT THE
BOTTOM7. AVOID CSS EXPRESSIONS8. MAKE JS AND CSS
EXTERNAL9. REDUCE DNS LOOKUPS10.MINIFY JS11.AVOID REDIRECTS12.REMOVE DUPLICATE
SCRIPTS13.CONFIGURE ETAGS14.MAKE AJAX CACHEABLE
Sept 2007
June 2009
Even Faster Web SitesSplitting the initial payloadLoading scripts without blockingCoupling asynchronous scriptsPositioning inline scriptsSharding dominant domainsFlushing the document earlyUsing iframes sparinglySimplifying CSS Selectors
Understanding Ajax performance..........Doug CrockfordCreating responsive web apps............Ben Galbraith, Dion
AlmaerWriting efficient JavaScript.............Nicholas ZakasScaling with Comet.....................Dylan SchiemannGoing beyond gzipping...............Tony GentilcoreOptimizing images...................Stoyan Stefanov, Nicole
Sullivan
AOLeBayFacebookMySpaceWikipediaYahoo!
Why focus on JavaScript?
YouTube
scripts block
<script src="A.js"> blocks parallel downloads and rendering
7 secs: IE 8, FF 3.5, Chr 2, Saf 4
9 secs: IE 6-7, FF 3.0, Chr 1, Op 9-10, Saf 3
JavaScript
Functions Executed before
onload
www.aol.com 115K 30%
www.ebay.com 183K 44%
www.facebook.com 1088K 9%
www.google.com/search
15K 45%
search.live.com/results
17K 24%
www.msn.com 131K 31%
www.myspace.com 297K 18%
en.wikipedia.org/wiki 114K 32%
www.yahoo.com 321K 13%
www.youtube.com 240K 18%
26% avg252K avg
initial payload and execution
splitting the initial payload
split your JavaScript between what's needed to render the page and everything else
load "everything else" after the page is rendered
separate manually (Firebug); tools needed to automate this (Doloto from Microsoft)
load scripts without blocking – how?
MSNScripts and other resources downloaded in parallel! How? Secret sauce?!var p= g.getElementsByTagName("HEAD")[0];var c=g.createElement("script");c.type="text/javascript";c.onreadystatechange=n;c.onerror=c.onload=k;c.src=e;p.appendChild(c)
MSN.com: parallel scripts
Loading Scripts Without Blocking
XHR Eval
XHR Injection
Script in Iframe
Script DOM Element
Script Defer
document.write Script Tag
XHR Eval
script must have same domain as main page
must refactor script
var xhrObj = getXHRObject();xhrObj.onreadystatechange = function() { if ( xhrObj.readyState != 4 ) return; eval(xhrObj.responseText); };xhrObj.open('GET', 'A.js', true);xhrObj.send('');
XHR Injectionvar xhrObj = getXHRObject();xhrObj.onreadystatechange = function() { if ( xhrObj.readyState != 4 ) return; var se=document.createElement('script'); document.getElementsByTagName('head') [0].appendChild(se); se.text = xhrObj.responseText; };xhrObj.open('GET', 'A.js', true);xhrObj.send('');
script must have same domain as main page
Script in Iframe<iframe src='A.html' width=0 height=0 frameborder=0 id=frame1></iframe>
iframe must have same domain as main page
must refactor script:// access iframe from main pagewindow.frames[0].createNewDiv();
// access main page from iframeparent.document.createElement('div');
Script DOM Elementvar se = document.createElement('script');se.src = 'http://anydomain.com/A.js';document.getElementsByTagName('head')[0].appendChild(se);
script and main page domains can differ
no need to refactor JavaScript
<script defer src='A.js'></script>
supported in IE and FF 3.1+
script and main page domains can differ
no need to refactor JavaScript
Script Defer
document.write("<script type='text/javascript' src='A.js'> <\/script>");
parallelization only works in IE
parallel downloads for scripts, nothing else
all document.writes must be in same script block
document.write Script Tag
*Only other document.write scripts are downloaded in parallel (in the same script block).
Load Scripts Without Blocking
XHR EvalXHR InjectionScript in iframeScript DOM ElementScript Defer
Script DOM ElementScript Defer
Script DOM Element
Script DOM Element (FF)Script Defer (IE)
XHR EvalXHR InjectionScript in iframeScript DOM Element (IE)
XHR InjectionXHR EvalScript DOM Element (IE)
Managed XHR InjectionManaged XHR EvalScript DOM Element
Managed XHR InjectionManaged XHR Eval
Script DOM Element (FF)Script Defer (IE)Managed XHR EvalManaged XHR Injection
Script DOM Element (FF)Script Defer (IE)Managed XHR EvalManaged XHR Injection
different domains same domains
no order
preserve order
no order
no busyshow busy
show busyno busy
preserve order
and the winner is...
asynchronous JS example: menu.js
<script type="text/javascript">var domscript = document.createElement('script');domscript.src = "menu.js"; document.getElementsByTagName('head')
[0].appendChild(domscript);
var aExamples = [ ['couple-normal.php', 'Normal Script Src'], ['couple-xhr-eval.php', 'XHR Eval'], ... ['managed-xhr.php', 'Managed XHR'] ];
function init() { EFWS.Menu.createMenu('examplesbtn', aExamples);}
init();</script>
script DOM element approach
before
after
*Only other document.write scripts are downloaded in parallel (in the same script block).
!IE
Loading Scripts Without Blocking
what about
inlined code that depends on the script?
coupling techniques
hardcoded callback
window onload
timer
degrading script tags
script onload
technique 5: script onload<script type="text/javascript">var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];
function init() { EFWS.Menu.createMenu('examplesbtn', aExamples);}
var domscript = document.createElement('script');domscript.src = "menu.js";
domscript.onloadDone = false;domscript.onload = function() { if ( ! domscript.onloadDone ) { init(); } domscript.onloadDone = true; };domscript.onreadystatechange = function() { if ( "loaded" === domscript.readyState ) { if ( ! domscript.onloadDone ) { init(); } domscript.onloadDone = true; }}
document.getElementsByTagName('head')[0].appendChild(domscript);</script>
pretty nice, medium complexity
asynchronous loading & coupling
async technique: Script DOM Elementeasy, cross-browserdoesn't ensure script order
coupling technique: script onloadfairly easy, cross-browserensures execution order for external
script and inlined code
multiple interdependent external and inline scripts:much more complex (see hidden slides)concatenate your external scripts into
one!
focus on the frontend
run YSlow (http://developer.yahoo.com/yslow)
and Page Speed! (http://code.google.com/speed/page-speed/)
speed matters
takeaways
Bing:
Yahoo:
Google:
AOL:
Shopzilla:
1 http://en.oreilly.com/velocity2009/public/schedule/detail/8523 2 http://www.slideshare.net/stoyan/yslow-20-presentation3 http://en.oreilly.com/velocity2009/public/schedule/detail/75794 http://en.oreilly.com/velocity2009/public/schedule/detail/7709
+2000 ms -4.3% revenue/user1
+400 ms -5-9% full-page traffic2
+400 ms -0.59% searches/user1
fastest users +50% page views3
-5000 ms +7-12% revenue4
impact on revenue
hardware – reduced loadShopzilla – 50% fewer servers
bandwidth – reduced response size
http://billwscott.com/share/presentations/2008/stanford/HPWP-RealWorld.pdf
cost savings
if you want better user experience more revenue reduced operating costs
the strategy is clear
Even Faster Web Sites