55
Fast Loading JavaScript http://www.flickr.com/photos/gaelenh/ 1443926963/ Velocity EU 2011, @aaronpeters

Fast Loading JavaScript

Embed Size (px)

DESCRIPTION

An walk-through of several JavaScript loading techniques with a characteristics table for each and at the end a decision tree to help you decide which technique to use. Also, Chrome's silly preload logic!

Citation preview

Page 1: Fast Loading JavaScript

Fast Loading JavaScript

http://www.flickr.com/photos/gaelenh/1443926963/

Velocity EU 2011, @aaronpeters

Page 2: Fast Loading JavaScript

Better performance==

More revenues

Revisorweb

Page 3: Fast Loading JavaScript

“I totally rock the house!”

http://www.flickr.com/photos/jkohen/3799706725/

Page 4: Fast Loading JavaScript

The problem

Page 5: Fast Loading JavaScript

JavaScript blocks

Page 6: Fast Loading JavaScript

Lots of JS blocks even more

Torstein Frogner

Page 7: Fast Loading JavaScript

Lots of JS blocks even more

Torstein Frogner

<script src=“file.js”> blocks parallel downloading in IE8 and Chrome (!)

can’t download subsequent images/iframes

… has “silly preload logic”

2

<script src=“file.js”> and inline scripts block HTML parsing & page rendering

1

Page 8: Fast Loading JavaScript

http://www.flickr.com/photos/frenkieb/4423393/

Example of bad, bad JS

Page 9: Fast Loading JavaScript

http://www.flickr.com/photos/frenkieb/4423393/

Example of bad, bad JS

Page 10: Fast Loading JavaScript

http://www.flickr.com/photos/frenkieb/4423393/

Example of bad, bad JS

Wanna see the horror?

Page 11: Fast Loading JavaScript

It’s not getting any better

Page 12: Fast Loading JavaScript

Requirements

Page 13: Fast Loading JavaScript

load JS in a non-blocking way1

scripts execute in order2

couple external JS with inline JS3

rendering starts soon; is progressive5

DOM ready fires asap4

Page 14: Fast Loading JavaScript

diskdepot.co.uk

You want to be in control

Page 15: Fast Loading JavaScript

Reduce risk to a minimum

http://www.flickr.com/photos/48329209@N03/4430804547/

Page 16: Fast Loading JavaScript

Create the best user experience!

http://www.flickr.com/photos/97469566@N00/4848627841

Page 17: Fast Loading JavaScript

Async FTW!

http://www.flickr.com/photos/15181848@N02/3742832809

Page 18: Fast Loading JavaScript

JavaScript Loading Techniques

Page 19: Fast Loading JavaScript

Normal Script Src

<script src=“foo.js"></script><script src=“bar.js"></script><script>// dependent on bar.jsexecuteAfterBar();</script>

Page 20: Fast Loading JavaScript

Normal Script Src

Chrome’s silly preload logic

Page 21: Fast Loading JavaScript

Chrome’s silly preload logic (CSPL)

If there is a non-DEFER, non-ASYNC parser-inserted script in

<head>, Chrome (15) only preloads other parser-inserted

scripts from <body>, not images!

Page 22: Fast Loading JavaScript

CSPL - Proofin <head> in <body>

Page 23: Fast Loading JavaScript

Browserscope doesn’t tell you

Browserscope testpage has

script in <body>

Page 24: Fast Loading JavaScript

Same in IE9?

Nope, all good!

Page 25: Fast Loading JavaScript

How about FF7?

Yeah, good too!

Page 26: Fast Loading JavaScript

Why CSPL is a problem

Other objects start downloading late

It’s against developer intent: bottom BODY

means “do it last, other stuff comes first”

Page 27: Fast Loading JavaScript

Solutions for CSPL

Add DEFER attribute

Add ASYNC attribute

Use Script Insertion

Move to top of <body>

Move to bottom of <body>

Inline the codeKeeps blocking

Start Render

Page 28: Fast Loading JavaScript

Pre-render blocking JS?- Inline in <head>- External file top of <body>

1

http://www.flickr.com/photos/valeriebb/290711738/

Page 29: Fast Loading JavaScript

Script Insertion

<script>var d=document, js=d.createElement('script'),

el=d.getElementsByTagName('script')[0];

js.src=“file.js";el.parentNode.insertBefore(js,el);</script>

Page 30: Fast Loading JavaScript

Script Insertion

Important!script can’t have document.write

Page 31: Fast Loading JavaScript

Script Insertion + callback()<script>function exec() { renderThingy(); }var d=document, js=d.createElement('script'), el=d.getElementsByTagName('script')[0];js.src=“getsatisfaction.js“;js.done=false; js.onload=function(){ js.done=true,exec() },js.onreadystatechange=function(){("loaded"===js.readyState||"complete"===js.readyState) && !js.done && (js.done=true,exec())};el.parentNode.insertBefore(js,el);</script>

Page 32: Fast Loading JavaScript

Script insertion is awesome. Make it your default

2

http://www.flickr.com/photos/valeriebb/290711738/

Page 33: Fast Loading JavaScript

DEFER attribute

<script defer src=“framework.js"></script><script defer src=“app.js"></script>

<script>// dependent on app.js

initApp();</script>

Page 34: Fast Loading JavaScript

DEFER attribute

Important!script can’t have document.write

Page 35: Fast Loading JavaScript

DEFER & jQuery in IE

<script defer src=“jquery.js"></script><script defer src=“jquery-plugin.js"></script>

‘jQuery’ is undefined

Page 36: Fast Loading JavaScript

Combine jquery.js and jquery-dependent.js if you want

DEFER

2

http://www.flickr.com/photos/valeriebb/290711738/

Page 37: Fast Loading JavaScript

ASYNC attribute

<script async src=“file.js"></script>

Page 38: Fast Loading JavaScript

ASYNC attribute

Important!script can’t have document.write

Page 39: Fast Loading JavaScript

Only use async as an ‘add-on’ in dynamic insertion technique

3

http://www.flickr.com/photos/valeriebb/290711738/

Page 40: Fast Loading JavaScript

ASYNC = false

<script>var d=document, js=d.createElement('script'),

el=d.getElementsByTagName('script')[0];

js.async=false; js.src=“file.js";el.parentNode.insertBefore(js,el);</script> <script async=“false”

src=“file.js"></script>

Page 41: Fast Loading JavaScript

ASYNC = false

Important!script can’t have document.write

Page 42: Fast Loading JavaScript

Forget about async=falseIt’s for the far future.

4

http://www.flickr.com/photos/valeriebb/290711738/

Page 43: Fast Loading JavaScript

LABjs

<script>$LAB .script("framework.js").wait() .script("plugin.framework.js") .wait(function(){ myplugin.init(); framework.init(); });</script>

<script src=“LABjs.js"></script>

Page 44: Fast Loading JavaScript

LABjs

Important!script can’t have document.write

Page 45: Fast Loading JavaScript

Script loaders like LABjs can be your best friend. Try it!

5

http://www.flickr.com/photos/valeriebb/290711738/

Page 46: Fast Loading JavaScript

Execute before Start Render?

<2k gzipped?

Inline, in <head>

Y N

Normal Script Src, top of

<body>

Y

Y

Couple with inline script?

Preserve exec order?

LABjs

Y

Dynamic insertion

N

DEFER

Y

N

Execute right before DCL?N

N

Using jQuery? Combine jquery.js & jquery-dependent.js

Other script loaders, like Yepnope, may do an equally good job

Page 47: Fast Loading JavaScript

Somewhat off-topic statements

Don’t load it if the page doesn’t need it !

Don’t use jQuery for everything

Do waterfall chart analysis ,‘till you drop Use Webpagetest.org (Firefox 7 coming soon to all

nodes!)

On WPT, use Video capturing to see how it renders

WPT has lots of useful commands in the API. Use them!

Page 48: Fast Loading JavaScript

Third Party JavaScript

Page 49: Fast Loading JavaScript

Social buttons BFF!<!-- facebook like --><div class="fb-like" data-send="false" data-

width="280"></div><!-- twitter --><a class="twitter-share-button" data-

count="horizontal">Tweet</a><!-- g+ --><div class="g-plusone" data-size="medium"></div>

http://www.phpied.com/social-button-bffs/

Page 50: Fast Loading JavaScript

Social buttons BFF!<!-- facebook like --><div class="fb-like" data-send="false" data-

width="280"></div><!-- twitter --><a class="twitter-share-button" data-

count="horizontal">Tweet</a><!-- g+ --><div class="g-plusone" data-size="medium"></div>

http://www.phpied.com/social-button-bffs/

<div id="fb-root"></div><!-- fb needs this --><script>(function(d, s) { var js, fjs = d.getElementsByTagName(s)[0], load =

function(url, id) { if (d.getElementById(id)) {return;} js = d.createElement(s); js.async = js.src = url; js.id = id; fjs.parentNode.insertBefore(js, fjs); }; load('//connect.facebook.net/en_US/all.js#xfbml=1',

'fbjssdk'); load('https://apis.google.com/js/plusone.js',

'gplus1js'); load('//platform.twitter.com/widgets.js', 'tweetjs');}(document, 'script'));</script>

Page 51: Fast Loading JavaScript

Twitter Anywhere Tweet Box

view-source:http://www.cdnplanet.com > jsbeautifier.org

<div id=“tbox"></script><script>twttr.anywhere(function (T) { T("#tbox").tweetBox(); });</script></body>

...<script src="http://platform.twitter.com/anywhere.js?id=YOUR_API_KEY&v=1"></script>

</head>...

Page 52: Fast Loading JavaScript

You?

Join the WPO community!You?

You? You?

You?

Page 53: Fast Loading JavaScript

#webperf http://twitter.com/search#webperf#WPOchat http://www.wpochat.orgPerfplanet http://perfplanet.comMeetup http://web-performance.meetup.com/

Page 54: Fast Loading JavaScript

http://www.flickr.com/photos/27282406@N03/4134166721/

Page 55: Fast Loading JavaScript

Questions?http://www.flickr.com/photos/f-oxymoron/5005146417/