39

Even Faster Web Sites at The Ajax Experience

Embed Size (px)

DESCRIPTION

Even Faster Web Sites covering loading scripts without blocking, coupling asynchronous scripts, and flushing the document early.

Citation preview

Page 1: Even Faster Web Sites at The Ajax Experience
Page 2: Even Faster Web Sites at The Ajax Experience

17%

83%

iGoogle, primed cache

the importance of frontend performance

9% 91%

iGoogle, empty cache

Page 3: Even Faster Web Sites at The Ajax Experience

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

Page 4: Even Faster Web Sites at The Ajax Experience

web performance guy

Page 5: Even Faster Web Sites at The Ajax Experience

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

Page 6: Even Faster Web Sites at The Ajax Experience

AOLeBayFacebookMySpaceWikipediaYahoo!

Why focus on JavaScript?

YouTube

Page 7: Even Faster Web Sites at The Ajax Experience

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

Page 8: Even Faster Web Sites at The Ajax Experience

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 9: Even Faster Web Sites at The Ajax Experience

Loading Scripts Without Blocking

XHR Eval

XHR Injection

Script in Iframe

Script DOM Element

Script Defer

document.write Script Tag

Page 10: Even Faster Web Sites at The Ajax Experience

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

Page 11: Even Faster Web Sites at The Ajax Experience

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

Page 12: Even Faster Web Sites at The Ajax Experience

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

Page 13: Even Faster Web Sites at The Ajax Experience

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

Page 14: Even Faster Web Sites at The Ajax Experience

<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

Page 15: Even Faster Web Sites at The Ajax Experience

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

Page 16: Even Faster Web Sites at The Ajax Experience

browser busy indicators

Page 17: Even Faster Web Sites at The Ajax Experience

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

Load Scripts Without Blocking

Page 18: Even Faster Web Sites at The Ajax Experience

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

Page 19: Even Faster Web Sites at The Ajax Experience

menu.jsimage.gif

Page 20: Even Faster Web Sites at The Ajax Experience

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

Page 21: Even Faster Web Sites at The Ajax Experience

before

after

menu.jsimage.gif

menu.jsimage.gif

Page 22: Even Faster Web Sites at The Ajax Experience

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

!IE

Loading Scripts Without Blocking

Page 23: Even Faster Web Sites at The Ajax Experience

what about

inlined code that depends on the script?

Page 24: Even Faster Web Sites at The Ajax Experience

coupling techniques

hardcoded callback

window onload

timer

degrading script tags

script onload

Page 25: Even Faster Web Sites at The Ajax Experience

John Resig's degrading script tags

<script src="menu-degrading.js" type="text/javascript">var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];

function init() { EFWS.Menu.createMenu('examplesbtn', aExamples);}

init();</script>

at the end of menu-degrading.js:var scripts =

document.getElementsByTagName("script");var cntr = scripts.length;while ( cntr ) { var curScript = scripts[cntr-1]; if (curScript.src.indexOf("menu-degrading.js") != -1) { eval( curScript.innerHTML ); break; } cntr--;}

http://ejohn.org/blog/degrading-script-tags/

cleanerclearersafer – inlined code not called if script

failsno browser supports it

Page 26: Even Faster Web Sites at The Ajax Experience

technique 4: degrading script tags

<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-degrading.js";if ( -1 != navigator.userAgent.indexOf("Opera") ) { domscript.innerHTML = "init();";}else { domscript.text = "init();";}document.getElementsByTagName('head')[0].appendChild(domscript);</script>

elegant, flexible (cool!)not well knowndoesn't work for 3rd party scripts

(unless...)

Page 27: Even Faster Web Sites at The Ajax Experience

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 complexitymenu.jsimage.gif

Page 28: Even Faster Web Sites at The Ajax Experience

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!

Page 29: Even Faster Web Sites at The Ajax Experience

flushing the document early

gotchas:PHP output_buffering – ob_flush()Transfer-Encoding: chunkedgzip – Apache's DeflateBufferSize before

2.2.8proxies and anti-virus softwarebrowsers – Safari (1K), Chrome (2K)

other languages: $| or FileHandle autoflush (Perl), flush

(Python), ios.flush (Ruby)

htmlimageimagescript

htmlimageimagescript call PHP's flush()

Page 30: Even Faster Web Sites at The Ajax Experience

flushing and domain blocking

you might need to move flushed resources to a domain different from the HTML dochtml

imageimagescript

htmlimageimagescript

blocked by HTML document

different domains

Page 31: Even Faster Web Sites at The Ajax Experience

successful flushing

Google Search

external resource downloaded earlycontent visible to the user

googleimageimagescriptimage204

http://www.google.com/images/nav_logo4.png

Page 32: Even Faster Web Sites at The Ajax Experience

YSlow

Page Speed

Pagetest VRTA neXpert

combine JS & CSS X X Xuse CSS sprites X Xuse a CDN X Xset Expires in the future

X X X X X

gzip text responses X X X X Xput CSS at the top X Xput JS at the bottom Xavoid CSS expressions X Xmake JS & CSS externalreduce DNS lookups X Xminify JS X X Xavoid redirects X X X Xremove dupe scripts Xremove ETags X X X

performance analyzers (HPWS)

Page 33: Even Faster Web Sites at The Ajax Experience

performance analyzers (EFWS)YSlo

wPage

SpeedPagetest VRTA neXper

tdon't block UI thread

split JS payload Xload scripts async X

inline JS b4 stylesheet Xwrite efficient JS

min. uncompressed size

optimize images X Xshard domains X X

flush the documentavoid iframes

simplify CSS selectors X X

Page 34: Even Faster Web Sites at The Ajax Experience

performance analyzers (other)YSlo

wPage

SpeedPagetest VRTA neXper

tuse persistent conns X X Xreduce cookies 2.0 X X Xavoid net congestion Xincrease MTU, TCP win

X

avoid server congestion

X

remove unused CSS Xspecify image dims Xuse GET for Ajax 2.0reduce DOM elements

2.0

avoid 404 errors 2.0avoid Alpha filters 2.0don't scale images 2.0 Xoptimize favicon 2.0

Page 35: Even Faster Web Sites at The Ajax Experience

focus on the frontend

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

and Page Speed! (http://code.google.com/speed/page-speed/)

speed matters

takeaways

Page 36: Even Faster Web Sites at The Ajax Experience

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

Page 37: Even Faster Web Sites at The Ajax Experience

hardware – reduced loadShopzilla – 50% fewer servers

bandwidth – reduced response size

http://billwscott.com/share/presentations/2008/stanford/HPWP-RealWorld.pdf

cost savings

Page 38: Even Faster Web Sites at The Ajax Experience

if you want better user experience more revenue reduced operating costs

the strategy is clear

Even Faster Web Sites

Page 39: Even Faster Web Sites at The Ajax Experience

Steve [email protected]

http://stevesouders.com/docs/tae-20090914.ppt

book signing immediately 3:30-3:50