420 likes | 435 Views
Learn about the significant impact of frontend performance on end-user experience and discover proven techniques to optimize website speed. Steve Souders shares practical tips and best practices for writing fast and efficient code.
E N D
Life's too short,write fast codepart 1 Steve Souders souders@google.com http://stevesouders.com/docs/myspace-20081024.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
Time Spent on the Frontend April 2008
The Performance Golden Rule 80-90% of the end-user response time is spent on the frontend. Start there. greater potential for improvement simpler proven to work
14 Rules • Make fewer HTTP requests • Use a CDN • Add an Expires header • Gzip components • Put stylesheets at the top • Put scripts at the bottom • Avoid CSS expressions • Make JS and CSS external • Reduce DNS lookups • Minify JS • Avoid redirects • Remove duplicate scripts • Configure ETags • Make AJAX cacheable
YSlow High Performance Web Sites
http://conferences.oreilly.com/velocity/ June 22-24, 2009
High Performance Web Sites, Vol 2 } • Split the initial payload • Load scripts without blocking • Don't scatter inline scripts • Split dominant domains • Make static content cookie-free • Reduce cookie weight • Minify CSS • Optimize images • Use iframes sparingly • To www or not to www part 1
Why focus on JavaScript? Yahoo! Wikipedia eBay AOL MySpace YouTube Facebook
Scripts Block <script src="A.js"> blocks parallel downloads and rendering http://stevesouders.com/cuzillion/?ex=10008 What's "Cuzillion"?
Initial Payload and Execution 26% avg 252K avg
Split 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?
MSN.com: Parallel Scripts MSN Scripts 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)
Advanced Script Loading XHR Eval XHR Injection Script in Iframe Script DOM Element Script Defer document.write Script Tag
XHR Eval varxhrObj = getXHRObject(); xhrObj.onreadystatechange = function() { if ( xhrObj.readyState != 4 ) return; eval(xhrObj.responseText); }; xhrObj.open('GET', 'A.js', true); xhrObj.send(''); script must have same domain as main page must refactor script http://stevesouders.com/cuzillion/?ex=10009
XHR Injection varxhrObj = 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 http://stevesouders.com/cuzillion/?ex=10015
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 page • window.frames[0].createNewDiv(); • // access main page from iframe • parent.document.createElement('div'); http://stevesouders.com/cuzillion/?ex=10012
Script DOM Element var 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 http://stevesouders.com/cuzillion/?ex=10010
Script Defer <script defer src='A.js'></script> only supported in IE (just landed in FF 3.1) script and main page domains can differ no need to refactor JavaScript http://stevesouders.com/cuzillion/?ex=10013
document.write Script Tag document.write("<scr" + "ipt type='text/javascript' src='A.js'>" + "</scr" + "ipt>"); parallelization only works in IE parallel downloads for scripts, nothing else all document.writes must be in same script block http://stevesouders.com/cuzillion/?ex=10014
Browser Busy Indicators good to show busy indicators when the user needs feedback bad when downloading in the background
Ensure/Avoid Ordered Execution • Ensure scripts execute in order: • necessary when scripts have dependencies • IE: http://stevesouders.com/cuzillion/?ex=10017 • FF: http://stevesouders.com/cuzillion/?ex=10018 • Avoid scripts executing in order: • faster – first script back is executed immediately • http://stevesouders.com/cuzillion/?ex=10019
Summary of Traits *Only other document.write scripts are downloaded in parallel (in the same script block).
and the winner is... XHR Eval XHR Injection Script in iframe Script DOM Element Script Defer same domains different domains Script DOM Element Script Defer no order preserve order XHR Eval XHR Injection Script in iframe Script DOM Element (IE) Script DOM Element (FF) Script Defer (IE) Managed XHR Eval Managed XHR Injection no order preserve order Script DOM Element no busy show busy Script DOM Element (FF) Script Defer (IE) Managed XHR Injection Managed XHR Eval Script DOM Element (FF) Script Defer (IE) Managed XHR Eval Managed XHR Injection no busy show busy XHR Injection XHR Eval Script DOM Element (IE) Managed XHR Injection Managed XHR Eval Script DOM Element
Load Scripts without Blocking don't let scripts block other downloads you can still control execution order, busy indicators, and onload event What about inline scripts?
Inline Scripts after Stylesheets Block Downloading Firefox 3 and IE download stylesheets in parallel ...unless the stylesheet is followed by an inline script http://stevesouders.com/cuzillion/?ex=10021 best to move inline scripts above stylesheets or below other resources use Link, not @import
Examples of Scattered Scripts MSN Wikipedia eBay MySpace
Don't Scatter Inline Scripts remember inline scripts carry a cost avoid long-executing inline scripts don't put inline scripts between stylesheets and other resources
Announcement 1: UA Profiler tracks browser performance traits http://stevesouders.com/ua/ go to the test page your browser automatically walks through the tests (requires JS) results recorded and shared publicly currently 2K+ tests, 1K+ unique testers, 100+ browsers help out by running the test!
Measuring Performance Episodes dev box synthetic testing bucket testing real user data Hammerhead
Announcement 2: Hammerhead"moving performance testing upstream" http://stevesouders.com/hammerhead/ Firebug extension load M URLs N times, empty & primed cache record average & median time add'l features: export data load time measurement modal cache clearing combine with bandwidth throttler
Splash YSlow: 74 unused JS: ~87% unused CSS: ~61%, 47K
Home YSlow: 49 unused JS: ~86% unused CSS: ~61%, 44K
Profile YSlow: 81 unused JS: ~91% unused CSS: ~69%, 43K
Splash YSlow: 60 unused JS: ~89% unused CSS: ~65%, 36K
MySpace performance analysis analyzed Splash, Home, Profile, More Pics the good • future Expires (10 days) • gzip • some CSS sprites • no CSS expressions • no redirects • bad ETags syntax changed
MySpace performance analysis the opportunities • YSlow: 74, 49, 81 60 added myspacecdn.com, x.myspace.com to CDN list • ads • combine scripts & stylesheets • split JS payload: ~90% unused (~360K uncomp) • remove unused CSS: ~65% (~50K uncomp) • move scripts to the bottom or load w/o blocking • minify scripts
Takeaways focus on the frontend run YSlow: http://developer.yahoo.com/yslow this year's focus: JavaScript Split the Initial Payload Load Scripts without Blocking Don't Scatter Inline Scripts speed matters life's too short, write fast code
Steve Souders souders@google.com http://stevesouders.com/docs/myspace-20081024.ppt