sxsw 20090314

Download Sxsw 20090314

Post on 07-May-2015

1.608 views

Category:

Technology

1 download

Embed Size (px)

TRANSCRIPT

  • 1.Steve Souders [email_address] http://stevesouders.com/docs/sxsw-20090314.ppt Even Faster Web Sites Disclaimer:This content does not necessarily reflect the opinions of my employer.

2. the importance of frontend performance 17% 83% iGoogle, primed cache 9% 91% iGoogle, empty cache 3. time spent on the frontend April 2008 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% 4. 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

5. 6. 7. 8. 25% discount code: "ssouders25" 9. Sept 2007 10. June 2009 11. Even Faster Websites Split the initial payload Load scripts without blocking Couple asynchronous scripts Don't scatter inline scripts Split the dominant domain Flush the document early Use iframes sparingly Simplify CSS Selectors Ajax performance (Doug Crockford) Writing efficient JavaScript (Nicholas Zakas) Creating responsive web apps (Ben Galbraith, Dion Almaer) Comet (Dylan Schiemann) Beyond Gzipping (Tony Gentilcore) Optimize Images (Stoyan Stefanov, Nicole Sullivan) 12. why focus on JavaScript? AOL eBay Facebook MySpace Wikipedia Yahoo! YouTube 13. scripts block 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 21. document.writeScript Tag document.write ("" + "" + "ipt>"); parallelization only works in IE parallel downloads for scripts, nothing else alldocument.write s must be in same script block http://stevesouders.com/cuzillion/?ex=10014 22. browser busy indicators 23. browser busy indicators goodto show busy indicators when the user needs feedback badwhen downloading in the background 24. 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

25. load scripts without blocking * Only other document.write scripts are downloaded in parallel (in the same script block). 26. and the winner is... XHR Eval XHR Injection Script in iframe Script DOM Element Script Defer Script DOM Element Script Defer Script DOM Element Script DOM Element (FF) Script Defer (IE) XHR Eval XHR Injection Script in iframe Script DOM Element (IE) XHR Injection XHR Eval Script DOM Element (IE) Managed XHR Injection Managed XHR Eval Script DOM Element Managed XHR Injection Managed XHR Eval Script DOM Element (FF) Script Defer (IE) Managed XHR Eval Managed XHR Injection Script DOM Element (FF) Script Defer (IE) Managed XHR Eval Managed XHR Injection different domains same domains no order preserve order no order no busy show busy show busy no busy preserve order 27. load scripts without blocking

  • don't let scripts block other downloads
  • you can still control execution order, busy indicators, and onload event
  • What about inline scripts?

28. 29. synchronous JS example: menu.js

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

30. asynchronous JS example: menu.js

  • 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 DOM element approach 31. before after 32. load scripts without blocking * Only other document.write scripts are downloaded in parallel (in the same script block). !IE 33. asynchronous scripts wrap-up

  • what about
  • inlined code
  • that depends on the script?

34.

  • what about
  • inlined code
  • that depends on the script?

35. baseline coupling results (not good) *Scripts download in parallel regardless of the Defer attribute. need a way to load scripts asynchronously AND preserve order 36. coupling techniques

  • hardcoded callback
  • window onload
  • timer
  • degrading script tags
  • script onload

37. technique 1: hardcoded callback

  • var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];
  • function init() {
  • EFWS.Menu.createMenu('examplesbtn', aExamples);
  • }
  • var domscript = document.createElement('script');
  • domscript.src = "menu.js";
  • document.getElementsByTagName('head')[0].appendChild(domscript);
  • init() is called from within menu.js
  • not very flexible
  • doesn't work for 3 rdparty scripts

38. technique 2: window onload

  • var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];
  • function init() {
  • EFWS.Menu.createMenu('examplesbtn', aExamples);
  • }
  • if ( window.addEventListener ) {
  • window.addEventListener("load", init, false);
  • }
  • else if ( window.attachEvent ) {
  • window.attachEvent("onload", init);
  • }
  • init() is called at window onload
  • must use async technique that blocks onload:
    • Script in Iframe does this across most browsers
  • init() called later than necessary

39. technique 3: timer

  • var domscript = document.createElement('script');
  • domscript.src = "menu.js";
  • document.getElementsByTagName('head')[0].appendChild(domscript);
  • var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];
  • function init() {
  • EFWS.Menu.createMenu('examplesbtn', aExamples);
  • }
  • function initTimer(interval) {
  • if ( "undefined" === typeof(EFWS) ) {
  • setTimeout(initTimer, interval);
  • }
  • else {
  • init();
  • }
  • }
  • initTimer(300);
  • load ifintervaltoo low, delay if too high
  • slight increased maintenance EFWS

40. John Resig's degrading script tags

  • var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];
  • function init() {
  • EFWS.Menu.createMenu('examplesbtn', aExamples);
  • }
  • init();

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/ cleaner clearer safer inlined code not called if script fails no browser supports it 41. technique 4: degrading script tags

  • 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);
  • elegant, flexible (cool!)
  • not well known
  • doesn't work for 3 rdparty scripts (unless...)

42. technique 5: script onload

  • var aExamples = [['couple-normal.php', 'Normal Script Src'], ...];
  • function init()