Fast Loading JavaScript

4 min read Original article ↗
  • 1.

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

  • 2.
  • 3.

    “ I totallyrock the house!” http://www.flickr.com/photos/jkohen/3799706725/

  • 4.
  • 5.
  • 6.

    Lots of JSblocks even more Torstein Frogner

  • 7.

    Lots of JSblocks 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

  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

    load JS ina non-blocking way 1 scripts execute in order 2 couple external JS with inline JS 3 rendering starts soon; is progressive 5 DOM ready fires asap 4

  • 14.
  • 15.

    Reduce risk toa minimum http://www.flickr.com/photos/48329209@N03/4430804547/

  • 16.

    Create the bestuser experience! http://www.flickr.com/photos/97469566@N00/4848627841

  • 17.
  • 18.
  • 19.

    Normal Script Src<script src =“foo.js&quot; ></script> <script src =“bar.js&quot; ></script> <script> // dependent on bar.js executeAfterBar(); </script>

  • 20.
  • 21.

    Chrome’s silly preloadlogic (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!

  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

    Why CSPL isa problem Other objects start downloading late It’s against developer intent: bottom BODY means “do it last, other stuff comes first”

  • 27.

    Solutions for CSPLMove to top of <body> Move to bottom of <body> Inline the code Add DEFER attribute Add ASYNC attribute Use Script Insertion Keeps blocking Start Render

  • 28.

    Pre-render blocking JS?Inline in <head> External file top of <body> 1 http://www.flickr.com/photos/valeriebb/290711738/

  • 29.

    Script Insertion <script>var d=document, js=d.createElement( 'script' ), el=d.getElementsByTagName( 'script' )[ 0 ]; js.src= “file.js&quot; ; el.parentNode.insertBefore(js,el); </script>

  • 30.
  • 31.

    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(){ (&quot;loaded&quot;===js.readyState||&quot;complete&quot;===js.readyState ) && !js.done && (js.done=true,exec())}; el.parentNode.insertBefore(js,el); </script>

  • 32.

    Script insertion isawesome. Make it your default 2 http://www.flickr.com/photos/valeriebb/290711738/

  • 33.

    DEFER attribute <script defer src =“framework.js&quot; ></script> <script defer src =“app.js&quot; ></script> <script> // dependent on app.js initApp(); </script>

  • 34.
  • 35.

    DEFER & jQueryin IE <script defer src =“jquery.js&quot; ></script> <script defer src =“jquery-plugin.js&quot; ></script> ‘ jQuery’ is undefined

  • 36.

    Combine jquery.js andjquery-dependent.js if you want DEFER 2 http://www.flickr.com/photos/valeriebb/290711738/

  • 37.
  • 38.
  • 39.

    Only use async as an ‘add-on ’ in dynamic insertion technique 3 http://www.flickr.com/photos/valeriebb/290711738/

  • 40.

    ASYNC = false<script> var d=document, js=d.createElement( 'script' ), el=d.getElementsByTagName( 'script' )[ 0 ]; js.async= false ; js.src= “file.js&quot; ; el.parentNode.insertBefore(js,el); </script> <script async =“false” src =“file.js&quot; ></script>

  • 41.

    ASYNC = falseImportant! script can’t have document.write

  • 42.

    Forget about async=false It’s for the far future. 4 http://www.flickr.com/photos/valeriebb/290711738/

  • 43.

    LABjs <script> $LAB.script( &quot;framework.js&quot; ).wait() .script( &quot;plugin.framework.js&quot; ) .wait( function (){ myplugin.init(); framework.init(); }); </script> <script src =“LABjs.js&quot; ></script>

  • 44.
  • 45.

    Script loaders likeLABjs can be your best friend. Try it! 5 http://www.flickr.com/photos/valeriebb/290711738/

  • 46.

    Execute before StartRender? <2k gzipped? Inline, in <head> Y Couple with inline script? Preserve exec order? N Using jQuery? Combine jquery.js & jquery-dependent.js Other script loaders, like Yepnope, may do an equally good job Y N Normal Script Src, top of <body> Y LABjs Y Dynamic insertion N DEFER Y N Execute right before DCL? N

  • 47.

    Somewhat off-topic statementsDon’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!

  • 48.
  • 49.

    Social buttons BFF!<!-- facebook like --> <div class = &quot;fb-like&quot; data-send= &quot;false&quot; data-width= &quot;280&quot; ></div> <!-- twitter --> <a class = &quot;twitter-share-button&quot; data-count= &quot;horizontal&quot; >Tweet</a> <!-- g+ --> <div class =&quot;g-plusone&quot; data-size= &quot;medium&quot; ></div> http://www.phpied.com/social-button-bffs/

  • 50.

    Social buttons BFF!<!-- facebook like --> <div class = &quot;fb-like&quot; data-send= &quot;false&quot; data-width= &quot;280&quot; ></div> <!-- twitter --> <a class = &quot;twitter-share-button&quot; data-count= &quot;horizontal&quot; >Tweet</a> <!-- g+ --> <div class =&quot;g-plusone&quot; data-size= &quot;medium&quot; ></div> http://www.phpied.com/social-button-bffs/ <div id =&quot;fb-root&quot; ></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>

  • 51.

    Twitter Anywhere TweetBox <div id =“tbox&quot; ></script> <script> twttr.anywhere( function (T) { T( &quot;#tbox&quot; ).tweetBox(); }); </script> </body> view-source:http://www.cdnplanet.com > jsbeautifier.org ... <script src=&quot;http://platform.twitter.com/anywhere.js?id=YOUR_API_KEY&v=1&quot;></script> </head> ...

  • 52.
  • 53.
  • 54.
  • 55.