SlideShare a Scribd company logo
High Performance
Snippets



        stevesouders.com/docs/fluent-snippets-20120530.pptx
      Disclaimer: This content does not necessarily reflect the opinions of my employer.
High Performance Snippets
synchronous scripts block
all following elements
from rendering
in all browsers
High Performance Snippets
async


#fail
async

           sync

   async
Frontend
 SPOF
http://www.webpagetest.org/result/120529_41_HWV/
http://blog.patrickmeenan.com/2011/10/testing-for-frontend-spof.html
/etc/hosts:
72.66.115.13 apis.google.com
72.66.115.13 www.google-analytics.com
72.66.115.13 static.ak.fbcdn.net
72.66.115.13 connect.facebook.net
72.66.115.13 platform.twitter.com
72.66.115.13 widgets.twimg.com


WebPagetest:
setDnsName apis.google.com blackhole.webpagetest.org
setDnsName www.google-analytics.com blackhole.webpagetest.org
setDnsName static.ak.fbcdn.net blackhole.webpagetest.org
setDnsName connect.facebook.net blackhole.webpagetest.org
setDnsName platform.twitter.com blackhole.webpagetest.org
setDnsName widgets.twimg.com blackhole.webpagetest.org
navigate http://www.businessinsider.com/
High Performance Snippets
High Performance Snippets
High Performance Snippets
original snippet
<script src="http://widgets.twimg.com/j/2/widget.js">
</script>
<script>
new TWTR.Widget({
  version: 2,
  type: 'profile',
  ...
}).render().setUser('souders').start();
</script>
<script>                           asyncified snippet
function doTwitter() {
  if ( this.readyState && this.readyState != "complete"
       && this.readyState != "loaded" ) {
    return;
  }
  this.onload = this.onreadystatechange = null;
  new TWTR.Widget({
    version: 2,
    type: 'profile',
    ...
  }).render().setUser('souders').start();
}
var ts = document.createElement("script");
ts.async = true;
ts.onload = ts.onreadystatechange = doTwitter;
ts.src = "http://widgets.twimg.com/j/2/widget.js";
var s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(ts, s0);
</script>
But... you can’t make a script async if it does
 document.write

widget.js does document.write:
init: function(x) {
  ...
  this.id = x.id || "twtr-widget-" + this._widgetNumber;
  if (!x.id) {
    document.write('<div class="twtr-widget" id="' +
                 this.id + '"></div>')
  }
}


What if we create the DIV and specify the id?
asyncified snippet plus DIV
<div class=‘twtr-widget’ id=‘souderstwitter’></div>
<script>
function doTwitter() {
  if ( this.readyState && this.readyState != "complete"
       && this.readyState != "loaded" ) {
    return;
  }
  this.onload = this.onreadystatechange = null;
  new TWTR.Widget({
    id: ‘souderstwitter’,
    version: 2,
    type: 'profile',
    ...
  }).render().setUser('souders').start();
}
var ts = document.createElement("script");
ts.async = true;
...
before
after
While placing JavaScript files at the bottom of the
page is a best practice for website performance,
when including the anywhere.js file, always place
the file as close to the top of the page as possible.
The anywhere.js file is small (<3KB) and is delivered
to the page GZIP'd.
Further, all dependancies for @Anywhere features
are loaded asynchronously, on-demand so as to not
impact performance of the host page.
three facts:
 1. failures happen
 2. 304 & 200 both produce
    frontend SPOF
 3. anywhere.js expires after
    15 minutes
snippet cache times
http://platform.twitter.com/widgets.js - 30 mins
http://connect.facebook.net/en_US/all.js - 15 mins
http://www.google-analytics.com/ga.js - 120 mins
“bootstrap scripts” often have short
 cache times
more frequent Conditional GET requests
 means frontend SPOF is more likely
but short cache times is the only way
 snippet owners can push updates
or is it?
self-updating bootstrap scripts




stevesouders.com/blog/2012/05/22/self-updating-scripts/
part 1: update notification
piggyback on dynamic request
pass cached version # to
 server: ?v=127
server returns:
 204 – no update
 JS – new version available
part 2: overwrite cache
re-request dynamically
rev the URL (querystring)
XHR w/ setRequestHeader
reload iframe containing
 bootstrap script
example
 load bootstrap script:
 var s1=document.createElement('script');
 s1.async=true;
 s1.src='http://souders.org/tests/selfupdating/boo
   tstrap.js’;
 var
   s0=document.getElementsByTagName('script’)[0];
 s0.parentNode.insertBefore(s1, s0);




stevesouders.com/tests/selfupdating/
send beacon:
 http://souders.org/tests/selfupdating/beacon.js?v
   =02:29:53


 beacon response triggers
  update:
 var iframe1 = document.createElement("iframe");
 iframe1.style.display = "none”;
 iframe1.src =
   "http://souders.org/tests/selfupdating/update.p
   hp?v=02:29:53";
 document.body.appendChild(iframe1);
stevesouders.com/tests/selfupdating/
iframe reloads itself:
 <script
   src="http://souders.org/tests/selfupdating/boot
   strap.js">
 </script>
 <script>
 if (location.hash === '') {
   location.hash = "check”;
   location.reload(true);
 }
 </script>

 reload triggers Conditional GET
 cached script is updated
stevesouders.com/tests/selfupdating/
voilà
bootstrap scripts can have long
 cache times
AND
get updated when necessary
caveats
the update is used on the next
 page view (like app cache)
1 reported IE8 issue
takeaways
load 3rd party scripts async
test your site with
 blackhole.webpagetest.org
have RUM timeout
make bootstrap scripts self-
 updating & increase cache
 times
High Performance Snippets
Steve Souders
                                        @souders
stevesouders.com/docs/fluent-snippets-20120530.pptx

More Related Content

What's hot (20)

PPTX
Browser Automated Testing Frameworks - Nightwatch.js
Luís Bastião Silva
 
PDF
High Performance JavaScript - WebDirections USA 2010
Nicholas Zakas
 
PPT
Web 2.0 Expo: Even Faster Web Sites
Steve Souders
 
PDF
Automatic Functional Testing with Selenium and SauceLabs
Joseph Chiang
 
PDF
20160905 - BrisJS - nightwatch testing
Vladimir Roudakov
 
PDF
Clojure Web Development
Hong Jiang
 
PPTX
@media - Even Faster Web Sites
Steve Souders
 
PDF
High Performance JavaScript (YUIConf 2010)
Nicholas Zakas
 
PDF
Performance on the Yahoo! Homepage
Nicholas Zakas
 
PPT
Responsive interfaces
Nicholas Zakas
 
PDF
Going Node At Netflix
Ryan Anklam
 
PPTX
JavaScript Performance (at SFJS)
Steve Souders
 
PDF
Cross-browser testing in the real world
Martin Kleppmann
 
PDF
Testing nightwatch, by David Torroija
David Torroija
 
PDF
Testing Web Applications
Seth McLaughlin
 
PPTX
Design+Performance
Steve Souders
 
PDF
High Performance JavaScript - Fronteers 2010
Nicholas Zakas
 
PPTX
JavaScript Timers, Power Consumption, and Performance
Nicholas Zakas
 
PDF
State of the resource timing api
Aaron Peters
 
PPTX
Automate testing with behat, selenium, phantom js and nightwatch.js (5)
Faichi Solutions
 
Browser Automated Testing Frameworks - Nightwatch.js
Luís Bastião Silva
 
High Performance JavaScript - WebDirections USA 2010
Nicholas Zakas
 
Web 2.0 Expo: Even Faster Web Sites
Steve Souders
 
Automatic Functional Testing with Selenium and SauceLabs
Joseph Chiang
 
20160905 - BrisJS - nightwatch testing
Vladimir Roudakov
 
Clojure Web Development
Hong Jiang
 
@media - Even Faster Web Sites
Steve Souders
 
High Performance JavaScript (YUIConf 2010)
Nicholas Zakas
 
Performance on the Yahoo! Homepage
Nicholas Zakas
 
Responsive interfaces
Nicholas Zakas
 
Going Node At Netflix
Ryan Anklam
 
JavaScript Performance (at SFJS)
Steve Souders
 
Cross-browser testing in the real world
Martin Kleppmann
 
Testing nightwatch, by David Torroija
David Torroija
 
Testing Web Applications
Seth McLaughlin
 
Design+Performance
Steve Souders
 
High Performance JavaScript - Fronteers 2010
Nicholas Zakas
 
JavaScript Timers, Power Consumption, and Performance
Nicholas Zakas
 
State of the resource timing api
Aaron Peters
 
Automate testing with behat, selenium, phantom js and nightwatch.js (5)
Faichi Solutions
 

Viewers also liked (6)

PPTX
do u webview?
Steve Souders
 
PPTX
The Perception of Speed
Steve Souders
 
PPTX
High Performance Web Components
Steve Souders
 
PPTX
How fast are we going now?
Steve Souders
 
PDF
Metrics of Joy
Steve Souders
 
PPTX
Design+Performance Velocity 2015
Steve Souders
 
do u webview?
Steve Souders
 
The Perception of Speed
Steve Souders
 
High Performance Web Components
Steve Souders
 
How fast are we going now?
Steve Souders
 
Metrics of Joy
Steve Souders
 
Design+Performance Velocity 2015
Steve Souders
 
Ad

Similar to High Performance Snippets (20)

PPTX
"Your script just killed my site" by Steve Souders
Dmitry Makarchuk
 
PPTX
JavaScript performance patterns
Stoyan Stefanov
 
PPTX
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Patrick Meenan
 
PPT
Sanjeev ghai 12
Praveen kumar
 
PPTX
JavaScript Performance Patterns
Stoyan Stefanov
 
KEY
Modern iframe programming
benvinegar
 
PDF
Js Saturday 2013 your jQuery could perform better
Ivo Andreev
 
PDF
Even faster web sites 1st Edition Steve Souders
huapepotts09
 
KEY
Optimization of modern web applications
Eugene Lazutkin
 
PPT
Velocity EU 2012 - Third party scripts and you
Patrick Meenan
 
ODP
Web program-peformance-optimization
xiaojueqq12345
 
ZIP
On Demand Javascript - Scalecamp 2009
Michael Mahemoff
 
KEY
Going on an HTTP Diet: Front-End Web Performance
Adam Norwood
 
PDF
PrairieDevCon 2014 - Web Doesn't Mean Slow
dmethvin
 
PDF
Building performance into the new yahoo homepage presentation
masudakram
 
PPT
Web performance testing
Patrick Meenan
 
PPTX
External JavaScript Widget Development Best Practices (updated) (v.1.1)
Volkan Özçelik
 
PDF
Are Today’s Good Practices… Tomorrow’s Performance Anti-Patterns?
Andy Davies
 
PPTX
High Performance Social Plugins
Stoyan Stefanov
 
PPTX
External JavaScript Widget Development Best Practices
Volkan Özçelik
 
"Your script just killed my site" by Steve Souders
Dmitry Makarchuk
 
JavaScript performance patterns
Stoyan Stefanov
 
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Patrick Meenan
 
Sanjeev ghai 12
Praveen kumar
 
JavaScript Performance Patterns
Stoyan Stefanov
 
Modern iframe programming
benvinegar
 
Js Saturday 2013 your jQuery could perform better
Ivo Andreev
 
Even faster web sites 1st Edition Steve Souders
huapepotts09
 
Optimization of modern web applications
Eugene Lazutkin
 
Velocity EU 2012 - Third party scripts and you
Patrick Meenan
 
Web program-peformance-optimization
xiaojueqq12345
 
On Demand Javascript - Scalecamp 2009
Michael Mahemoff
 
Going on an HTTP Diet: Front-End Web Performance
Adam Norwood
 
PrairieDevCon 2014 - Web Doesn't Mean Slow
dmethvin
 
Building performance into the new yahoo homepage presentation
masudakram
 
Web performance testing
Patrick Meenan
 
External JavaScript Widget Development Best Practices (updated) (v.1.1)
Volkan Özçelik
 
Are Today’s Good Practices… Tomorrow’s Performance Anti-Patterns?
Andy Davies
 
High Performance Social Plugins
Stoyan Stefanov
 
External JavaScript Widget Development Best Practices
Volkan Özçelik
 
Ad

More from Steve Souders (13)

PPTX
Make JavaScript Faster
Steve Souders
 
PPTX
High Performance Web Components
Steve Souders
 
PDF
Prebrowsing - Velocity NY 2013
Steve Souders
 
PPTX
High Performance Mobile (SF/SV Web Perf)
Steve Souders
 
PPTX
High Performance HTML5 (SF HTML5 UG)
Steve Souders
 
PPTX
Web Directions South - Even Faster Web Sites
Steve Souders
 
PPTX
Souders WPO Web 2.0 Expo
Steve Souders
 
PPTX
JSConf US 2010
Steve Souders
 
PDF
CouchDB Google
Steve Souders
 
PPT
Even Faster Web Sites at jQuery Conference '09
Steve Souders
 
PPTX
Browserscope Launch at TAE
Steve Souders
 
PPT
Even Faster Web Sites at The Ajax Experience
Steve Souders
 
PPT
SXSW: Even Faster Web Sites
Steve Souders
 
Make JavaScript Faster
Steve Souders
 
High Performance Web Components
Steve Souders
 
Prebrowsing - Velocity NY 2013
Steve Souders
 
High Performance Mobile (SF/SV Web Perf)
Steve Souders
 
High Performance HTML5 (SF HTML5 UG)
Steve Souders
 
Web Directions South - Even Faster Web Sites
Steve Souders
 
Souders WPO Web 2.0 Expo
Steve Souders
 
JSConf US 2010
Steve Souders
 
CouchDB Google
Steve Souders
 
Even Faster Web Sites at jQuery Conference '09
Steve Souders
 
Browserscope Launch at TAE
Steve Souders
 
Even Faster Web Sites at The Ajax Experience
Steve Souders
 
SXSW: Even Faster Web Sites
Steve Souders
 

Recently uploaded (20)

PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 

High Performance Snippets

  • 1. High Performance Snippets stevesouders.com/docs/fluent-snippets-20120530.pptx Disclaimer: This content does not necessarily reflect the opinions of my employer.
  • 3. synchronous scripts block all following elements from rendering in all browsers
  • 5. async #fail async sync async
  • 9. /etc/hosts: 72.66.115.13 apis.google.com 72.66.115.13 www.google-analytics.com 72.66.115.13 static.ak.fbcdn.net 72.66.115.13 connect.facebook.net 72.66.115.13 platform.twitter.com 72.66.115.13 widgets.twimg.com WebPagetest: setDnsName apis.google.com blackhole.webpagetest.org setDnsName www.google-analytics.com blackhole.webpagetest.org setDnsName static.ak.fbcdn.net blackhole.webpagetest.org setDnsName connect.facebook.net blackhole.webpagetest.org setDnsName platform.twitter.com blackhole.webpagetest.org setDnsName widgets.twimg.com blackhole.webpagetest.org navigate http://www.businessinsider.com/
  • 13. original snippet <script src="http://widgets.twimg.com/j/2/widget.js"> </script> <script> new TWTR.Widget({ version: 2, type: 'profile', ... }).render().setUser('souders').start(); </script>
  • 14. <script> asyncified snippet function doTwitter() { if ( this.readyState && this.readyState != "complete" && this.readyState != "loaded" ) { return; } this.onload = this.onreadystatechange = null; new TWTR.Widget({ version: 2, type: 'profile', ... }).render().setUser('souders').start(); } var ts = document.createElement("script"); ts.async = true; ts.onload = ts.onreadystatechange = doTwitter; ts.src = "http://widgets.twimg.com/j/2/widget.js"; var s0 = document.getElementsByTagName('script')[0]; s0.parentNode.insertBefore(ts, s0); </script>
  • 15. But... you can’t make a script async if it does document.write widget.js does document.write: init: function(x) { ... this.id = x.id || "twtr-widget-" + this._widgetNumber; if (!x.id) { document.write('<div class="twtr-widget" id="' + this.id + '"></div>') } } What if we create the DIV and specify the id?
  • 16. asyncified snippet plus DIV <div class=‘twtr-widget’ id=‘souderstwitter’></div> <script> function doTwitter() { if ( this.readyState && this.readyState != "complete" && this.readyState != "loaded" ) { return; } this.onload = this.onreadystatechange = null; new TWTR.Widget({ id: ‘souderstwitter’, version: 2, type: 'profile', ... }).render().setUser('souders').start(); } var ts = document.createElement("script"); ts.async = true; ...
  • 18. after
  • 19. While placing JavaScript files at the bottom of the page is a best practice for website performance, when including the anywhere.js file, always place the file as close to the top of the page as possible. The anywhere.js file is small (<3KB) and is delivered to the page GZIP'd. Further, all dependancies for @Anywhere features are loaded asynchronously, on-demand so as to not impact performance of the host page.
  • 20. three facts: 1. failures happen 2. 304 & 200 both produce frontend SPOF 3. anywhere.js expires after 15 minutes
  • 21. snippet cache times http://platform.twitter.com/widgets.js - 30 mins http://connect.facebook.net/en_US/all.js - 15 mins http://www.google-analytics.com/ga.js - 120 mins
  • 22. “bootstrap scripts” often have short cache times more frequent Conditional GET requests means frontend SPOF is more likely but short cache times is the only way snippet owners can push updates or is it?
  • 24. part 1: update notification piggyback on dynamic request pass cached version # to server: ?v=127 server returns: 204 – no update JS – new version available
  • 25. part 2: overwrite cache re-request dynamically rev the URL (querystring) XHR w/ setRequestHeader reload iframe containing bootstrap script
  • 26. example load bootstrap script: var s1=document.createElement('script'); s1.async=true; s1.src='http://souders.org/tests/selfupdating/boo tstrap.js’; var s0=document.getElementsByTagName('script’)[0]; s0.parentNode.insertBefore(s1, s0); stevesouders.com/tests/selfupdating/
  • 27. send beacon: http://souders.org/tests/selfupdating/beacon.js?v =02:29:53 beacon response triggers update: var iframe1 = document.createElement("iframe"); iframe1.style.display = "none”; iframe1.src = "http://souders.org/tests/selfupdating/update.p hp?v=02:29:53"; document.body.appendChild(iframe1); stevesouders.com/tests/selfupdating/
  • 28. iframe reloads itself: <script src="http://souders.org/tests/selfupdating/boot strap.js"> </script> <script> if (location.hash === '') { location.hash = "check”; location.reload(true); } </script> reload triggers Conditional GET cached script is updated stevesouders.com/tests/selfupdating/
  • 29. voilà bootstrap scripts can have long cache times AND get updated when necessary
  • 30. caveats the update is used on the next page view (like app cache) 1 reported IE8 issue
  • 31. takeaways load 3rd party scripts async test your site with blackhole.webpagetest.org have RUM timeout make bootstrap scripts self- updating & increase cache times
  • 33. Steve Souders @souders stevesouders.com/docs/fluent-snippets-20120530.pptx

Editor's Notes

  • #2: flickr.com/photos/bestrated1/2141687384/
  • #3: WidgetsAdsAnalytics
  • #11: http://www.webpagetest.org/result/120530_HS_4BA/
  • #13: http://www.webpagetest.org/result/120530_7R_4BJ/
  • #18: http://www.webpagetest.org/result/120530_7R_4BJ/
  • #19: http://www.webpagetest.org/result/120530_8D_4BQ/
  • #22: flickr.com/photos/wwarby/3296379139/
  • #23: flickr.com/photos/juditk/5024772809/whether it’s 1 or 5 requests, frontend SPOF will still happen
  • #32: flickr.com/photos/myklroventine/4062102754/