450 likes | 642 Views
High Performance Widgets. Steve Souders souders@google.com http://stevesouders.com/docs/widget-summit-2008.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.
E N D
High Performance Widgets Steve Souders souders@google.com http://stevesouders.com/docs/widget-summit-2008.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
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"?
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
Get Satisfaction Widget <div id="gsfn_content">Loading...</div> <script_src="http://getsatisfaction.com/oreilly/widgets/javascripts/5977e91/widgets.js" type="text/javascript"></script> <script_src="http://getsatisfaction.com/oreilly/topics.widget?callback=gsfnTopicsCallback&length=120&limit=5&sort=recently_active" type="text/javascript"></script> ... function gsfn_populate(id, template) { document.getElementById(id).innerHTML = template; }
3 requests 8K xfer size 8K JS (uncompr) good • small improve • add future Expires header – widgets.js, favicon.gif (not topics.widget) • gzip – wigets.js, topics.widget • minify widgets.js • remove ETags • use only one: getsatisfaction.com or www.getsatisfaction.com • load scripts async
Google Calendar Widget <iframe_ src="//www.google.com/calendar/embed ?mode=AGENDA &height=600 &wkst=1 &bgcolor=%23FFFFFF &src=v9ke3029e8k53qjtbf506jumu8ktapjh%40import.calendar.google.com &color=%235A6986 &ctz=America%2FLos_Angeles" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"> </iframe>
10 requests 65K xfer size 135K JS (uncompr) good • gzip turned on • no ETags • future Expires headers improve • big! 135K JS, 18K CSS (uncompressed) • set iframe SRC dynamically • 44% of JS unused • sprite 6 images into 1 • /feeds/ is slow (only 2K, but longer than 133K JS)
Google Friend Connect Widget <div id="div-08054659623916314855-members"></div> <script type="text/javascript"> var skin = {}; skin['HEIGHT'] = '150'; skin['HEADER_FOOTER_BG_COLOR'] = '#BFEECC'; skin['MEMBERS_LIST_BG_COLOR'] = '#D9FFE4'; skin['MEMBERS_LIST_FRIEND_LINK_COLOR'] = '#339966'; skin['MEMBERS_LIST_HEADER_FONT_COLOR'] = '#339966'; skin['INVITE_LINK_COLOR'] = '#339966'; google.friendconnect.container.renderMembersGadget( { id: 'div-08054659623916314855-members', site: '08054659623916314855' }, skin); </script>
39 requests 188K xfer size 471K JS (uncompr) (3 widgets) good • gzip turned on • no ETags • future Expires headers improve • big! (really 3 widgets) • 3 redirects – max-age=0 • 74% of JS unused • 22 requests expire in 24 hours • 173K of images – lossless compression cuts 87% • 4 text/html responses not gzipped (total 19K) • 6 CSS expressions • 3 duplicate scripts – iframes
MyBlogLog Widget varmbl_widget = ''; mbl_widget += ''; mbl_widget += ' <style>'; mbl_widget += ' a.mbl_widget {…}'; mbl_widget += ' a.mbl_widget_u {…}'; mbl_widget += ' a.mbl_widget_a {…}'; mbl_widget += ''; [321 lines later...] mbl_widget += ' </div>'; mbl_widget += ' </div>'; mbl_widget += ''; document.write(mbl_widget);
js 14 requests 60K xfer size 62K JS (uncompr) good • sprite (v3_1_default_black.gif) • domain sharding • images optimized improve • add future Expires (nopic_48.gif, close12_1.gif) • 5 DNS lookups • avoid document.write • 80% of JS unused • load script asynchronously
Plaxo Widget <div id="plaxo-pulse-widget"></div> <script_src="http://pulse.plaxo.com/pulse/widget/get?authTicket=265...4%3D&type=frame&version=2&height=480&width=125"></script> iframe is appended to div
js 14 requests 41K xfer size 2K JS (uncompr) good • 10 day future Expires • gzip enabled • domain sharding (Flickr) • backend stitching improve • /badge is slow – start with previous request • do away with iframe altogether – one script • load script asynchronously – set size of div • 4 images could be sprited into 1
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 ~3K tests, ~2K unique testers, ~120 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
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/widget-summit-2008.ppt