SlideShare une entreprise Scribd logo
1  sur  46
Optimization of
modern web apps

     Eugene Lazutkin

   ClubAjax on 8/7/2012
       Dallas, TX
About me
• Eugene Lazutkin
• Open Source web developer
   • Majors: JavaScript & Python, Dojo & Django.
• Independent consultant
• Blog: lazutkin.com
• Twitter: @uhop, Google+: gplus.to/uhop
What’s new?
Browser landscape has
      changed
What else is new?

• We are transitioning from static web applications
  with JavaScript helpers to dynamic web
  applications.
   • One-page web applications.
   • Interactive grids, charts, CRUD.
   • Multimedia is on its way.
The waterfall
Important things
• Individual web requests.
• Event: DOMContentLoaded
   • DOM is fully parsed, but not laid out yet.
• Event: load
   • All external assets are loaded.
   • DOM geometry is calculated.
When can we use it?

• After DOMContentLoaded.
• After load.
• Sometimes between them.
   • Example: our app works, yet some images are
     being downloaded.
Problem: batching

• A network diagram frequently looks like a staircase.
• Requests are batched.
• Browser limits connections per host.
   • Usually 2-8 connections depending on browser
     and on HTTP version (1.0 or 1.1).
   • Prevents server overload.
Problem: bandwidth


• We need to download a lot of resources.
• Slow connections limit the app.
• The less we download, the better.
Anatomy of connection
• Lifecycle (add browser delays, and network latency
  liberally):
   1.Client does a DNS lookup (complex operation).
   2.Client sends a request (data, headers, cookies).
   3.Server gets it, processes it, and sends a
     response.
   4.Client receives and processes it.
Problem: connections


• Connections are expensive.
• We should reduce their number.
Solution: batching

• Sharding:
   • Serve different resources from different hosts.
• Pro: if batching is a bottleneck, that can help
  considerably.
• Con: more expensive DNS lookups.
CDN
• Can help with batching and bandwidth at the same
  time.
• Can reduce latency for geographically distributed
  clients.
• The same problems as sharding.
• You should factor in CDN service costs (usually
  inexpensive).
Solution: bandwidth
• Let’s compress all we can.
   • Images can be compressed lossy or losslessly.
   • Text (JavaScript, CSS, HTML) should be
     gzipped.
      • It can be preprocessed (minified) to be even
        more compressible.
• Use static compression whenever you can.
Solution: bandwidth

• If we bundle similar resources together usually we
  can compress them better.
   • Merge all JavaScript.
   • Merge all CSS.
   • Use sprites instead of separate images.
• Bundling conserves connections too!
Solution: bandwidth

• It makes sense to remove all inlined JavaScript
  (<script> blocks, event handlers), and CSS
  (<style> blocks) from HTML.
• Images should be converted to sprites.
   • Example: <img> can be represented as <div>
     with a proper background image.
What I use

• Both Dojo and RequireJS come with a build tool.
   • It bundles, and minifies JavaScript.
   • It bundles and minifies CSS.
• SmartSprites (http://csssprites.org)
   • It can handle vertically and horizontally tiled,
     and untiled images.
Problems with
           bundling

• 3rd party resources cannot be bundled easily.
• Bundled resources should have the same
  expiration.
• Dynamic data cannot be easily bundled.
Solution: connections


• We bundled all we could. Now what?
• Now it is time to go back to basics: network
  protocols starting with TCP/IP.
Really???
Oh, yes!

• The standard-compliant server sends 3 (three)
  packages and waits for ACK.
   • It is a part of congestion-controlling algorithm.
• What does it mean for us?
   • Client gets 3 packages relatively fast.
   • Useful payload is just over 1.5k.
TCP 3 packets rule

• How can we use it?
   • If we send an HTML page, try to fit all
     external resource requests in the first 1.5k.
   • If you can keep your HTML page under 1.5k
     (compressed) — awesome!
HTTP rules!

• HTTP/1.0 creates one connection per request.
   • Expensive.
• HTTP/1.1 allows to reuse the same connection to
  request different resources from the same host.
   • Double check that you use HTTP/1.1 on
     server.
HTTP pipelining
HTTP pipelining
• Part of HTTP/1.1.
• Allows to request several resources without waiting
  for response.
• Resources should come in the order of their
  requests.
• Frequently turned off.
• Improves high-latency/mobile scenarios.
SPDY

• Introduced by Google.
• Will likely be a part of HTTP/2.0.
• Allows asynchronous requests/responses over a
  single connection.
• Allows server push and server hint.
Who supports SPDY?

• Implemented by Chrome/Chromium and Firefox.
• Used by Google, Twitter.
• Announced by Facebook.
• Implemented by most vendors including Apache,
  nginx (experimental), most app servers like node.js.
• Server push and hint are rarely implemented.
Ideal web app
<!doctype html>
<html>
     <head>
        <link rel=”stylesheet” type=”text/css” href=”x.css”>
        <!— images are requested from CSS as one sprite —>
        <script src=”x.js”></script>
     </head>
     <body>
        <!— HTML here may be dynamically generated —>
     </body>
</html>


                          Now what?
Where to include JS?
• Most gurus recommend to include it in a body as a
  last node.
• That’s incorrect in general!
• It works only for “gradual enhancements” scripts.
   • Scripts, which provide convenience, not main
     functionality.
      • Error checking, calendars, and so on.
Where to include JS?

• It is unwise to make it last, if our app functionality
  depends on it.
    • It renders significant parts of out web page.
    • It requests data from a server.
    • It is the application.
• In our “ideal app” it doesn’t matter where to put it.
Can we reduce it more?
• We can inline CSS and JavaScript back.
• Images can be inlined too using “data:” URI.
• Cons:
   • Usually it violates “the same expiration” rule.
   • Prevents reuse between pages.
   • “data:” URI can increase a file size.
Problem: dynamic data
• We optimized the web app. Now what?
• Usually the dynamic data requests stick out like a
  sore thumb.
   • Unlike static files, such requests do take some
     server processing:
      • SQL queries, disk I/O, internal network
        services.
Solution: dynamic data

• We can try to consolidate several requests required
  to render a page into one request.
• We can request this data first thing.
   • Literally.
   • Both XHR and <script> can be used but I
     prefer scripts with JSONP.
Data-first idea part 1

• Let’s request the data first, if it takes a long time.
• In order to be efficient we cannot rely on any other
  JavaScript libraries.
• It will be loaded in parallel with the rest.
    • Con: it will occupy a connection slot.
• The result would be stored in a variable.
Data-first idea part 2

• When our main JS is loaded we can check that
  variable.
   • If it is populated, we can wait until DOM is
     ready to render data.
   • Otherwise we can override our JSONP callback
     function, and wait for data, and for DOM.
Data-first sketch
<!doctype html>
<html>
     <head>
        <script>
           function __load(data){...}
           var t = document.createElement("script");
           t.src = "/api?timeout=2&callback=__load";
           document.documentElement.appendChild(t);
        </script>
        <link...>
        <script...>
     </head>
     <body>
        <!— HTML, if any —>
Cache considerations


• If we expect our user to come again, or
• If we expect it to use other pages of our web app.
• We have to work with cache.
Server-side headers
• Determine expirations of your resources and set all
  proper HTTP headers:
   • Expires, Last-Modified, Cache-Control
   • If set properly, browser would not even attempt
     to connect within their expiration period.
• Set ETag header.
   • Sometimes timestamp is not reliable.
Server-side headers
• Proper settings reduce number of connections.
• It allows server to send 304 (not modified) response
  instead of a potentially big resource.
• Don’t forget that some companies and ISPs run
  intermediate caches.
• Read http://lazutkin.com/blog/2007/feb/1/
  improving-performance/ for more details.
Prime cache
• Sometimes it makes sense to load files not used by
  this web page, which can be used by other pages.
• Usually it is done asynchronously several seconds
  later after the page has started.
   • Invisible image can be created.
   • CSS and JS can be linked.
     • They should not interfere with the page!
Or use manifest

• Part of HTML5 to facilitate offline applications.
• A text file that lists what should be downloaded and
  placed into a permanent cache, network URLs,
  and fallback URLs.
• Should be served as “text/cache-manifest”.
• Supported by FF, Cr, Opera, Safari, IE10.
Cache manifest
            example
<!doctype html>
<html manifest=”cache.manifest”>
     ...

  <!— cache.manifest example content —>
  CACHE MANIFEST
  /y.js
  /y.css
  /y.jpg
  /y.html
  ...
Tools of trade

• Built-in debuggers of modern browsers.
   • Firebug.
• Network sniffers.
   • HTTPWatch, Fiddler.
• And...
Navigation timing


• For your debugging pleasure you can use
  Navigation Timing API.
• A lot of resource-specific timing information!
• Supported by FF, Cr, IE9.
That’s all
  folks!
Picture credits


pie: http://en.wikipedia.org/wiki/File:Wikimedia_browser_share_pie_chart_3.png
really: http://www.flickr.com/photos/zpeckler/3093588439/
http pipelining: http://en.wikipedia.org/wiki/File:HTTP_pipelining2.svg

Contenu connexe

Tendances

SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...Sencha
 
Synapseindia dot net development web applications with ajax
Synapseindia dot net development  web applications with ajaxSynapseindia dot net development  web applications with ajax
Synapseindia dot net development web applications with ajaxSynapseindiappsdevelopment
 
Catch 22: FLex APps
Catch 22: FLex APpsCatch 22: FLex APps
Catch 22: FLex APpsYash Mody
 
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi   SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi Sencha
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPressTaylor Lovett
 
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Binary Studio
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchTaylor Lovett
 
Adobe AEM CQ5 - Developer Introduction
Adobe AEM CQ5 - Developer IntroductionAdobe AEM CQ5 - Developer Introduction
Adobe AEM CQ5 - Developer IntroductionYash Mody
 
JavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User ExperienceJavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User Experiencereeder29
 
You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHPTaylor Lovett
 
Adobe AEM for Business Heads
Adobe AEM for Business HeadsAdobe AEM for Business Heads
Adobe AEM for Business HeadsYash Mody
 
Drupal, Android and iPhone
Drupal, Android and iPhoneDrupal, Android and iPhone
Drupal, Android and iPhoneAlexandru Badiu
 
Real World Rails Deployment
Real World Rails DeploymentReal World Rails Deployment
Real World Rails DeploymentAlan Hecht
 
Krug Fat Client
Krug Fat ClientKrug Fat Client
Krug Fat ClientPaul Klipp
 
Meanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore ChandraMeanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore ChandraKishore Chandra
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesWebStackAcademy
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsGary Yeh
 
Microservices with Apache Camel, Docker and Fabric8 v2
Microservices with Apache Camel, Docker and Fabric8 v2Microservices with Apache Camel, Docker and Fabric8 v2
Microservices with Apache Camel, Docker and Fabric8 v2Christian Posta
 

Tendances (20)

SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
SenchaCon 2016: LinkRest - Modern RESTful API Framework for Ext JS Apps - Rou...
 
Synapseindia dot net development web applications with ajax
Synapseindia dot net development  web applications with ajaxSynapseindia dot net development  web applications with ajax
Synapseindia dot net development web applications with ajax
 
Catch 22: FLex APps
Catch 22: FLex APpsCatch 22: FLex APps
Catch 22: FLex APps
 
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi   SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
SenchaCon 2016: The Modern Toolchain - Ross Gerbasi
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with Elasticsearch
 
Adobe AEM CQ5 - Developer Introduction
Adobe AEM CQ5 - Developer IntroductionAdobe AEM CQ5 - Developer Introduction
Adobe AEM CQ5 - Developer Introduction
 
JavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User ExperienceJavaScript Service Worker Design Patterns for Better User Experience
JavaScript Service Worker Design Patterns for Better User Experience
 
You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHP
 
Adobe AEM for Business Heads
Adobe AEM for Business HeadsAdobe AEM for Business Heads
Adobe AEM for Business Heads
 
Drupal, Android and iPhone
Drupal, Android and iPhoneDrupal, Android and iPhone
Drupal, Android and iPhone
 
Real World Rails Deployment
Real World Rails DeploymentReal World Rails Deployment
Real World Rails Deployment
 
Krug Fat Client
Krug Fat ClientKrug Fat Client
Krug Fat Client
 
Meanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore ChandraMeanstack Introduction by Kishore Chandra
Meanstack Introduction by Kishore Chandra
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
 
Node and Azure
Node and AzureNode and Azure
Node and Azure
 
Microservices with Apache Camel, Docker and Fabric8 v2
Microservices with Apache Camel, Docker and Fabric8 v2Microservices with Apache Camel, Docker and Fabric8 v2
Microservices with Apache Camel, Docker and Fabric8 v2
 
Java script
Java scriptJava script
Java script
 

Similaire à Optimization of modern web applications

Drupal is not your Website
Drupal is not your Website Drupal is not your Website
Drupal is not your Website Phase2
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQLKonstantin Gredeskoul
 
Drupal Is Not Your Web Site
Drupal Is Not Your Web SiteDrupal Is Not Your Web Site
Drupal Is Not Your Web SitePhase2
 
High performance website
High performance websiteHigh performance website
High performance websiteChamnap Chhorn
 
Building Lightning Fast Websites (for Twin Cities .NET User Group)
Building Lightning Fast Websites (for Twin Cities .NET User Group)Building Lightning Fast Websites (for Twin Cities .NET User Group)
Building Lightning Fast Websites (for Twin Cities .NET User Group)strommen
 
5 Common Mistakes You are Making on your Website
 5 Common Mistakes You are Making on your Website 5 Common Mistakes You are Making on your Website
5 Common Mistakes You are Making on your WebsiteAcquia
 
PAC 2019 virtual Mark Tomlinson
PAC 2019 virtual Mark TomlinsonPAC 2019 virtual Mark Tomlinson
PAC 2019 virtual Mark TomlinsonNeotys
 
How_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_FarmHow_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_FarmNigel Price
 
Cvcc performance tuning
Cvcc performance tuningCvcc performance tuning
Cvcc performance tuningJohn McCaffrey
 
Website & Internet + Performance testing
Website & Internet + Performance testingWebsite & Internet + Performance testing
Website & Internet + Performance testingRoman Ananev
 
AD113 Speed Up Your Applications w/ Nginx and PageSpeed
AD113  Speed Up Your Applications w/ Nginx and PageSpeedAD113  Speed Up Your Applications w/ Nginx and PageSpeed
AD113 Speed Up Your Applications w/ Nginx and PageSpeededm00se
 
Best practices-wordpress-enterprise
Best practices-wordpress-enterpriseBest practices-wordpress-enterprise
Best practices-wordpress-enterpriseTaylor Lovett
 
Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Timothy Fisher
 
Configuring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web PerormanceConfiguring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web PerormanceSpark::red
 
Going on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web PerformanceGoing on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web PerformanceAdam Norwood
 

Similaire à Optimization of modern web applications (20)

Drupal is not your Website
Drupal is not your Website Drupal is not your Website
Drupal is not your Website
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
Drupal Is Not Your Web Site
Drupal Is Not Your Web SiteDrupal Is Not Your Web Site
Drupal Is Not Your Web Site
 
High performance website
High performance websiteHigh performance website
High performance website
 
Building Lightning Fast Websites (for Twin Cities .NET User Group)
Building Lightning Fast Websites (for Twin Cities .NET User Group)Building Lightning Fast Websites (for Twin Cities .NET User Group)
Building Lightning Fast Websites (for Twin Cities .NET User Group)
 
5 Common Mistakes You are Making on your Website
 5 Common Mistakes You are Making on your Website 5 Common Mistakes You are Making on your Website
5 Common Mistakes You are Making on your Website
 
PAC 2019 virtual Mark Tomlinson
PAC 2019 virtual Mark TomlinsonPAC 2019 virtual Mark Tomlinson
PAC 2019 virtual Mark Tomlinson
 
How_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_FarmHow_To_Soup_Up_Your_Farm
How_To_Soup_Up_Your_Farm
 
Cvcc performance tuning
Cvcc performance tuningCvcc performance tuning
Cvcc performance tuning
 
Website & Internet + Performance testing
Website & Internet + Performance testingWebsite & Internet + Performance testing
Website & Internet + Performance testing
 
Javascript for Wep Apps
Javascript for Wep AppsJavascript for Wep Apps
Javascript for Wep Apps
 
WebDev Crash Course
WebDev Crash CourseWebDev Crash Course
WebDev Crash Course
 
AD113 Speed Up Your Applications w/ Nginx and PageSpeed
AD113  Speed Up Your Applications w/ Nginx and PageSpeedAD113  Speed Up Your Applications w/ Nginx and PageSpeed
AD113 Speed Up Your Applications w/ Nginx and PageSpeed
 
Best practices-wordpress-enterprise
Best practices-wordpress-enterpriseBest practices-wordpress-enterprise
Best practices-wordpress-enterprise
 
performance.ppt
performance.pptperformance.ppt
performance.ppt
 
Remix
RemixRemix
Remix
 
Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011
 
Configuring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web PerormanceConfiguring Apache Servers for Better Web Perormance
Configuring Apache Servers for Better Web Perormance
 
Going on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web PerformanceGoing on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web Performance
 
CI_CONF 2012: Scaling - Chris Miller
CI_CONF 2012: Scaling - Chris MillerCI_CONF 2012: Scaling - Chris Miller
CI_CONF 2012: Scaling - Chris Miller
 

Plus de Eugene Lazutkin

Functional practices in JavaScript
Functional practices in JavaScriptFunctional practices in JavaScript
Functional practices in JavaScriptEugene Lazutkin
 
Express: the web server for node.js
Express: the web server for node.jsExpress: the web server for node.js
Express: the web server for node.jsEugene Lazutkin
 
Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Eugene Lazutkin
 
SSJS, NoSQL, GAE and AppengineJS
SSJS, NoSQL, GAE and AppengineJSSSJS, NoSQL, GAE and AppengineJS
SSJS, NoSQL, GAE and AppengineJSEugene Lazutkin
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Eugene Lazutkin
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part IEugene Lazutkin
 
Dojo GFX workshop slides
Dojo GFX workshop slidesDojo GFX workshop slides
Dojo GFX workshop slidesEugene Lazutkin
 
Dojo GFX: SVG in the real world
Dojo GFX: SVG in the real worldDojo GFX: SVG in the real world
Dojo GFX: SVG in the real worldEugene Lazutkin
 
DojoX GFX Session Eugene Lazutkin SVG Open 2007
DojoX GFX Session Eugene Lazutkin SVG Open 2007DojoX GFX Session Eugene Lazutkin SVG Open 2007
DojoX GFX Session Eugene Lazutkin SVG Open 2007Eugene Lazutkin
 
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007Eugene Lazutkin
 

Plus de Eugene Lazutkin (19)

Service workers
Service workersService workers
Service workers
 
Advanced I/O in browser
Advanced I/O in browserAdvanced I/O in browser
Advanced I/O in browser
 
Streams
StreamsStreams
Streams
 
Functional practices in JavaScript
Functional practices in JavaScriptFunctional practices in JavaScript
Functional practices in JavaScript
 
Express: the web server for node.js
Express: the web server for node.jsExpress: the web server for node.js
Express: the web server for node.js
 
TXJS 2013 in 10 minutes
TXJS 2013 in 10 minutesTXJS 2013 in 10 minutes
TXJS 2013 in 10 minutes
 
Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
Pulsar
PulsarPulsar
Pulsar
 
SSJS, NoSQL, GAE and AppengineJS
SSJS, NoSQL, GAE and AppengineJSSSJS, NoSQL, GAE and AppengineJS
SSJS, NoSQL, GAE and AppengineJS
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)
 
RAD CRUD
RAD CRUDRAD CRUD
RAD CRUD
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
CRUD with Dojo
CRUD with DojoCRUD with Dojo
CRUD with Dojo
 
Dojo GFX workshop slides
Dojo GFX workshop slidesDojo GFX workshop slides
Dojo GFX workshop slides
 
Dojo GFX: SVG in the real world
Dojo GFX: SVG in the real worldDojo GFX: SVG in the real world
Dojo GFX: SVG in the real world
 
Dojo (QCon 2007 Slides)
Dojo (QCon 2007 Slides)Dojo (QCon 2007 Slides)
Dojo (QCon 2007 Slides)
 
DojoX GFX Session Eugene Lazutkin SVG Open 2007
DojoX GFX Session Eugene Lazutkin SVG Open 2007DojoX GFX Session Eugene Lazutkin SVG Open 2007
DojoX GFX Session Eugene Lazutkin SVG Open 2007
 
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
 

Dernier

Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Dernier (20)

Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

Optimization of modern web applications

  • 1. Optimization of modern web apps Eugene Lazutkin ClubAjax on 8/7/2012 Dallas, TX
  • 2. About me • Eugene Lazutkin • Open Source web developer • Majors: JavaScript & Python, Dojo & Django. • Independent consultant • Blog: lazutkin.com • Twitter: @uhop, Google+: gplus.to/uhop
  • 4. What else is new? • We are transitioning from static web applications with JavaScript helpers to dynamic web applications. • One-page web applications. • Interactive grids, charts, CRUD. • Multimedia is on its way.
  • 6. Important things • Individual web requests. • Event: DOMContentLoaded • DOM is fully parsed, but not laid out yet. • Event: load • All external assets are loaded. • DOM geometry is calculated.
  • 7. When can we use it? • After DOMContentLoaded. • After load. • Sometimes between them. • Example: our app works, yet some images are being downloaded.
  • 8. Problem: batching • A network diagram frequently looks like a staircase. • Requests are batched. • Browser limits connections per host. • Usually 2-8 connections depending on browser and on HTTP version (1.0 or 1.1). • Prevents server overload.
  • 9. Problem: bandwidth • We need to download a lot of resources. • Slow connections limit the app. • The less we download, the better.
  • 10. Anatomy of connection • Lifecycle (add browser delays, and network latency liberally): 1.Client does a DNS lookup (complex operation). 2.Client sends a request (data, headers, cookies). 3.Server gets it, processes it, and sends a response. 4.Client receives and processes it.
  • 11. Problem: connections • Connections are expensive. • We should reduce their number.
  • 12. Solution: batching • Sharding: • Serve different resources from different hosts. • Pro: if batching is a bottleneck, that can help considerably. • Con: more expensive DNS lookups.
  • 13. CDN • Can help with batching and bandwidth at the same time. • Can reduce latency for geographically distributed clients. • The same problems as sharding. • You should factor in CDN service costs (usually inexpensive).
  • 14. Solution: bandwidth • Let’s compress all we can. • Images can be compressed lossy or losslessly. • Text (JavaScript, CSS, HTML) should be gzipped. • It can be preprocessed (minified) to be even more compressible. • Use static compression whenever you can.
  • 15. Solution: bandwidth • If we bundle similar resources together usually we can compress them better. • Merge all JavaScript. • Merge all CSS. • Use sprites instead of separate images. • Bundling conserves connections too!
  • 16. Solution: bandwidth • It makes sense to remove all inlined JavaScript (<script> blocks, event handlers), and CSS (<style> blocks) from HTML. • Images should be converted to sprites. • Example: <img> can be represented as <div> with a proper background image.
  • 17. What I use • Both Dojo and RequireJS come with a build tool. • It bundles, and minifies JavaScript. • It bundles and minifies CSS. • SmartSprites (http://csssprites.org) • It can handle vertically and horizontally tiled, and untiled images.
  • 18. Problems with bundling • 3rd party resources cannot be bundled easily. • Bundled resources should have the same expiration. • Dynamic data cannot be easily bundled.
  • 19. Solution: connections • We bundled all we could. Now what? • Now it is time to go back to basics: network protocols starting with TCP/IP.
  • 21. Oh, yes! • The standard-compliant server sends 3 (three) packages and waits for ACK. • It is a part of congestion-controlling algorithm. • What does it mean for us? • Client gets 3 packages relatively fast. • Useful payload is just over 1.5k.
  • 22. TCP 3 packets rule • How can we use it? • If we send an HTML page, try to fit all external resource requests in the first 1.5k. • If you can keep your HTML page under 1.5k (compressed) — awesome!
  • 23. HTTP rules! • HTTP/1.0 creates one connection per request. • Expensive. • HTTP/1.1 allows to reuse the same connection to request different resources from the same host. • Double check that you use HTTP/1.1 on server.
  • 25. HTTP pipelining • Part of HTTP/1.1. • Allows to request several resources without waiting for response. • Resources should come in the order of their requests. • Frequently turned off. • Improves high-latency/mobile scenarios.
  • 26. SPDY • Introduced by Google. • Will likely be a part of HTTP/2.0. • Allows asynchronous requests/responses over a single connection. • Allows server push and server hint.
  • 27. Who supports SPDY? • Implemented by Chrome/Chromium and Firefox. • Used by Google, Twitter. • Announced by Facebook. • Implemented by most vendors including Apache, nginx (experimental), most app servers like node.js. • Server push and hint are rarely implemented.
  • 28. Ideal web app <!doctype html> <html> <head> <link rel=”stylesheet” type=”text/css” href=”x.css”> <!— images are requested from CSS as one sprite —> <script src=”x.js”></script> </head> <body> <!— HTML here may be dynamically generated —> </body> </html> Now what?
  • 29. Where to include JS? • Most gurus recommend to include it in a body as a last node. • That’s incorrect in general! • It works only for “gradual enhancements” scripts. • Scripts, which provide convenience, not main functionality. • Error checking, calendars, and so on.
  • 30. Where to include JS? • It is unwise to make it last, if our app functionality depends on it. • It renders significant parts of out web page. • It requests data from a server. • It is the application. • In our “ideal app” it doesn’t matter where to put it.
  • 31. Can we reduce it more? • We can inline CSS and JavaScript back. • Images can be inlined too using “data:” URI. • Cons: • Usually it violates “the same expiration” rule. • Prevents reuse between pages. • “data:” URI can increase a file size.
  • 32. Problem: dynamic data • We optimized the web app. Now what? • Usually the dynamic data requests stick out like a sore thumb. • Unlike static files, such requests do take some server processing: • SQL queries, disk I/O, internal network services.
  • 33. Solution: dynamic data • We can try to consolidate several requests required to render a page into one request. • We can request this data first thing. • Literally. • Both XHR and <script> can be used but I prefer scripts with JSONP.
  • 34. Data-first idea part 1 • Let’s request the data first, if it takes a long time. • In order to be efficient we cannot rely on any other JavaScript libraries. • It will be loaded in parallel with the rest. • Con: it will occupy a connection slot. • The result would be stored in a variable.
  • 35. Data-first idea part 2 • When our main JS is loaded we can check that variable. • If it is populated, we can wait until DOM is ready to render data. • Otherwise we can override our JSONP callback function, and wait for data, and for DOM.
  • 36. Data-first sketch <!doctype html> <html> <head> <script> function __load(data){...} var t = document.createElement("script"); t.src = "/api?timeout=2&callback=__load"; document.documentElement.appendChild(t); </script> <link...> <script...> </head> <body> <!— HTML, if any —>
  • 37. Cache considerations • If we expect our user to come again, or • If we expect it to use other pages of our web app. • We have to work with cache.
  • 38. Server-side headers • Determine expirations of your resources and set all proper HTTP headers: • Expires, Last-Modified, Cache-Control • If set properly, browser would not even attempt to connect within their expiration period. • Set ETag header. • Sometimes timestamp is not reliable.
  • 39. Server-side headers • Proper settings reduce number of connections. • It allows server to send 304 (not modified) response instead of a potentially big resource. • Don’t forget that some companies and ISPs run intermediate caches. • Read http://lazutkin.com/blog/2007/feb/1/ improving-performance/ for more details.
  • 40. Prime cache • Sometimes it makes sense to load files not used by this web page, which can be used by other pages. • Usually it is done asynchronously several seconds later after the page has started. • Invisible image can be created. • CSS and JS can be linked. • They should not interfere with the page!
  • 41. Or use manifest • Part of HTML5 to facilitate offline applications. • A text file that lists what should be downloaded and placed into a permanent cache, network URLs, and fallback URLs. • Should be served as “text/cache-manifest”. • Supported by FF, Cr, Opera, Safari, IE10.
  • 42. Cache manifest example <!doctype html> <html manifest=”cache.manifest”> ... <!— cache.manifest example content —> CACHE MANIFEST /y.js /y.css /y.jpg /y.html ...
  • 43. Tools of trade • Built-in debuggers of modern browsers. • Firebug. • Network sniffers. • HTTPWatch, Fiddler. • And...
  • 44. Navigation timing • For your debugging pleasure you can use Navigation Timing API. • A lot of resource-specific timing information! • Supported by FF, Cr, IE9.
  • 45. That’s all folks!

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n