widget summit 2008

45
Steve Souders [email protected] http://stevesouders.com/docs/widget-summit-2008.ppt High Performance Widgets Disclaimer: This content does not necessarily reflect the opinions of my

Upload: volkan-unsal

Post on 07-May-2015

1.703 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Widget Summit 2008

Steve [email protected]

http://stevesouders.com/docs/widget-summit-2008.ppt

High Performance Widgets

Disclaimer: This content does not necessarily reflect the opinions of my employer.

Page 2: Widget Summit 2008

17%

83%

iGoogle, primed cache

The Importance of Frontend Performance

9% 91%

iGoogle, empty cache

Page 3: Widget Summit 2008

Time Spent on the Frontend

Empty Cache

Primed Cache

www.aol.com 97% 97%

www.ebay.com 95% 81%

www.facebook.com 95% 81%

www.google.com/search

47% 0%

search.live.com/results 67% 0%

www.msn.com 98% 94%

www.myspace.com 98% 98%

en.wikipedia.org/wiki 94% 91%

www.yahoo.com 97% 96%

www.youtube.com 98% 97%April 2008

Page 4: Widget Summit 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

Page 5: Widget Summit 2008

14 Rules1. Make fewer HTTP

requests2. 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

Page 6: Widget Summit 2008

High Performance Web Sites

YSlow

Page 7: Widget Summit 2008

http://conferences.oreilly.com/velocity/ June 22-24, 2009

Page 8: Widget Summit 2008
Page 9: Widget Summit 2008

High Performance Web Sites, Vol 2

1. Split the initial payload2. Load scripts without blocking3. Don't scatter inline scripts4. Split dominant domains5. Make static content cookie-free6. Reduce cookie weight7. Minify CSS8. Optimize images9. Use iframes sparingly10.To www or not to www

Page 10: Widget Summit 2008

AOLeBayFacebookMySpaceWikipediaYahoo!

Why focus on JavaScript?

YouTube

Page 11: Widget Summit 2008

Scripts Block

<script src="A.js"> blocks parallel downloads and rendering

What's "Cuzillion"?

http://stevesouders.com/cuzillion/?ex=10008

Page 12: Widget Summit 2008

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

Page 13: Widget Summit 2008

Advanced Script Loading

XHR Eval

XHR Injection

Script in Iframe

Script DOM Element

Script Defer

document.write Script Tag

Page 14: Widget Summit 2008

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('');

http://stevesouders.com/cuzillion/?ex=10009

Page 15: Widget Summit 2008

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 pagehttp://stevesouders.com/cuzillion/?ex=10015

Page 16: Widget Summit 2008

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');

http://stevesouders.com/cuzillion/?ex=10012

Page 17: Widget Summit 2008

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

http://stevesouders.com/cuzillion/?ex=10010

Page 18: Widget Summit 2008

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

Page 19: Widget Summit 2008

document.write Script Tagdocument.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

Page 20: Widget Summit 2008

Browser Busy Indicators

Page 21: Widget Summit 2008

Browser Busy Indicators

good to show busy indicators when the user needs feedback

bad when downloading in the background

Page 22: Widget Summit 2008

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

immediatelyhttp://stevesouders.com/cuzillion/?ex=10019

Ensure/Avoid Ordered Execution

Page 23: Widget Summit 2008

Summary of Traits

*Only other document.write scripts are downloaded in parallel (in the same script block).

Page 24: Widget Summit 2008

and the winner is...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

Page 25: Widget Summit 2008

Load Scripts without Blocking

don't let scripts block other downloads

you can still control execution order, busy indicators, and onload event

Page 26: Widget Summit 2008

Get Satisfaction Widget

Page 27: Widget Summit 2008

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&amp;length=120&amp;limit=5&amp;sort=recently_active" type="text/javascript"></script>

...

function gsfn_populate(id, template) {

document.getElementById(id).innerHTML = template;

}

Page 28: Widget Summit 2008

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

3 requests8K xfer size8K JS

(uncompr)

Page 29: Widget Summit 2008

Google Calendar Widget

Page 30: Widget Summit 2008

Google Calendar Widget

<iframe_src="//www.google.com/calendar/embed

?mode=AGENDA&amp;height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=v9ke3029e8k53qjtbf506jumu8ktapjh

%40import.calendar.google.com&amp;color=%235A6986&amp;ctz=America%2FLos_Angeles"

style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no">

</iframe>

Page 31: Widget Summit 2008

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)

10 requests65K xfer size135K JS

(uncompr)

Page 32: Widget Summit 2008

Google Friend Connect Widget

Page 33: Widget Summit 2008

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>

Page 34: Widget Summit 2008

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

39 requests188K xfer size471K JS

(uncompr)(3 widgets)

Page 35: Widget Summit 2008

MyBlogLog Widget

Page 36: Widget Summit 2008

MyBlogLog Widget

var mbl_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);

Page 37: Widget Summit 2008

js14 requests60K xfer size62K 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

Page 38: Widget Summit 2008

Plaxo Widget

Page 39: Widget Summit 2008

Plaxo Widget

<div id="plaxo-pulse-widget"></div><script_src="http://pulse.plaxo.com/pulse/widget/get?authTicket=265...4%3D&amp;type=frame&amp;version=2&amp;height=480&amp;width=125"></script>

iframe is appended to div

Page 40: Widget Summit 2008

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

js14 requests41K xfer size2K JS

(uncompr)

Page 41: Widget Summit 2008

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!

Page 42: Widget Summit 2008

Measuring Performance

Episodes

dev box synthetic testing

bucket testing

real user data

Hammerhead

Page 43: Widget Summit 2008

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 dataload time measurementmodal cache clearing

combine with bandwidth throttler

Page 44: Widget Summit 2008

Takeaways

focus on the frontend

run YSlow: http://developer.yahoo.com/yslow

this year's focus: JavaScriptSplit the Initial PayloadLoad Scripts without BlockingDon't Scatter Inline Scripts

speed matters

life's too short, write fast code

Page 45: Widget Summit 2008

Steve [email protected]

http://stevesouders.com/docs/widget-summit-2008.ppt