KEMBAR78
Web Optimization Level: Paranoid | KEY
WEB OPTIMIZATION:
 PARANOID MODE
   ROBIN@MINDTALK.COM
WHY SPEED MATTER?
“If it is fast and ugly,
 they will use it and
       curse you;
if it is slow, they will
       not use it.”
  --David Cheriton
WEB OPTIMIZATION
WEB OPTIMIZATION

• Minimizecost of investment by reducing
 processor and bandwidth usage.
WEB OPTIMIZATION

• Minimizecost of investment by reducing
 processor and bandwidth usage.

• User   satisfaction and customer engagement.
WEB OPTIMIZATION

• Minimizecost of investment by reducing
 processor and bandwidth usage.

• User   satisfaction and customer engagement.

• SEO:   Influence rank.
WHAT SHOULD WE DO?
OPTIMIZATION LEVEL:
     BEGINNER
• Avoid    redirection as possible.

• Client
       side caching: cache your static files
 includes: images, javascript, and stylesheet (css).

 • for example http header based by adding Etag
   or Last-Modified http header.

• Reduce    the number of DOM elements.
OPTIMIZATION LEVEL:
     MEDIUM
Minify your javascript and css

function login(){
  var inputUname = document.getElementById(“username”);
  var inputPass = document.getElementById(“password”);
  ajaxPost(“/login”, {“username”:inputUname,”password”:inputPass});
}




            function a(){
              var b, c, d;
              d = document.getElementById;
              b = d(“username”);
              c = d(“password”);
              ap(“/login”, {“username”:b,”password”:c});
            }
Minify your javascript and css
    Benchmark: compressing mindtalk.js
          original size: 4464 KB

      Tool            Minified    Save
 YUI Compressor        3440 KB   22%



  Google Closure
                       2712 KB   39%
Simple Optimization




  Google Closure
    Advanced           556 KB    87%
   Optimization
YUI Compressor
Google Closure Simple Optimization
Google Closure Advanced Optimization
Server side compression
              with Gzip




http://www.sendung.de/wp-content/compression.png
Server side compression
                    with Gzip


mindtalk.min.js       GZIP        mindtalk.min.js
       556 KB                     164 KB


                             NETWORK
“Wait..., how about performance?”
Server side compression
                     with Gzip

• Dynamic    Gzip Compression.

• Static   Gzip Compression.
Server side compression
                with Gzip
HOWTO Enable Static Gzip compression (NGINX):
Server side compression
                    with Gzip
HOWTO Enable Static Gzip compression (NGINX):

• Add   `gzip_static on;` in your nginx config file.
Server side compression
                    with Gzip
HOWTO Enable Static Gzip compression (NGINX):

• Add   `gzip_static on;` in your nginx config file.

• Compress   your static files manually, example:
Server side compression
                    with Gzip
HOWTO Enable Static Gzip compression (NGINX):

• Add   `gzip_static on;` in your nginx config file.

• Compress   your static files manually, example:

 •$gzip js/mindtalk.min.js > js/
  mindtalk.min.js.gz
Server side compression
                    with Gzip
HOWTO Enable Static Gzip compression (NGINX):

• Add   `gzip_static on;` in your nginx config file.

• Compress   your static files manually, example:

 •$gzip js/mindtalk.min.js > js/
  mindtalk.min.js.gz

• Done.
OPTIMIZATION LEVEL:
    ADVANCED
Image Compression
• Use   Progressive Compression for JPEG files.
Image Compression

HOWTO Use Progressive Compression
Combine javascript / CSS
Combine javascript / CSS
               HOWTO:

$ cat jquery.js jquery-plugins-wow.js jquery-
plugins-box.js > all.js
Chunking content (flush) socket
            write.
   1

                                 5
                        3

    2

                                 6

                        4
Chunking content (flush) socket
            write.



        Ruby     ios.flush



        PHP       flush();



       Python     flush()
DATABASE OPTIMIZATION
DATABASE OPTIMIZATION



Know your self and your enemy and you win!
                 --Sun Tzu

  Know your database and the bottleneck
             and you win!
              --unknown
DATABASE OPTIMIZATION
    HOWTO (MongoDB):
DATABASE OPTIMIZATION
                  HOWTO (MongoDB):

• Profiling   manually by setting profiling level:
DATABASE OPTIMIZATION
                  HOWTO (MongoDB):

• Profiling   manually by setting profiling level:

 •>   db.setProfilingLevel(2);
DATABASE OPTIMIZATION
                  HOWTO (MongoDB):

• Profiling   manually by setting profiling level:

 •>   db.setProfilingLevel(2);

• Profiling
         automatically by using Dex (MongoDB
 Index bot).
DATABASE OPTIMIZATION
                  HOWTO (MongoDB):

• Profiling   manually by setting profiling level:

 •>   db.setProfilingLevel(2);

• Profiling
         automatically by using Dex (MongoDB
 Index bot).

 •$ dex -f /var/log/mongodb.log mongodb://
  localhost/db
OPTIMIZATION LEVEL:
    PARANOID
USE NON BLOCKING I/O
THREAD BASED I/O

 worker worker worker worker




Client   Client   Client   Client   Client
THREAD BASED I/O

 worker worker worker worker




                                     WAITING

Client   Client   Client   Client   Client
NON BLOCKING EVENT BASED I/O




EVENT EMITTERS        IO LOOP                     EVENT HANDLER


                        Event Queue
              REQ
                                            REQ

                                      REQ
                       REQ    REQ




         Client     Client   Client    Client        Client
NON BLOCKING EVENT BASED I/O




               “CONFUSING!!!”
NON BLOCKING EVENT BASED I/O




$(document).ready(function(){
     alert(“document loaded”);
});
$(“.button”).click(function(){
     alert(“button clicked”);
});
NON BLOCKING EVENT BASED I/O




var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
NON BLOCKING EVENT BASED I/O
NON BLOCKING EVENT BASED I/O
NON BLOCKING EVENT BASED I/O
NON BLOCKING EVENT BASED I/O
USE ASYNCHRONOUS FOR
              COSTLY OPERATION

• Notification.

• Search.

• Stream.

• Logging   via network. eg: Scribe.
LAZY LOAD IMAGES
CLIENT SIDE TEMPLATING
WHY CLIENT SIDE TEMPLATING?


•   Extensible: You can change the template without change the
    logic.

•   Expressive: As expressive as desktop application.

•   Cacheable: Provide our template to the browser once.

•   Bandwidth friendly: Reduce amount of bandwidth usage by
    just providing raw data (JSON) to browser and let browser
    render the dom element.

•   Minifiable.
WHY CLIENT SIDE TEMPLATING?
WHY CLIENT SIDE TEMPLATING?




        jTemplate
             vs
          PURE
             vs
     Closure Template
WHY CLIENT SIDE TEMPLATING?
HOWTO (CLOSURE TEMPLATE):
HOWTO (CLOSURE TEMPLATE):
HOWTO (CLOSURE TEMPLATE):
HOWTO (CLOSURE TEMPLATE):
HOWTO (CLOSURE TEMPLATE):
HOWTO (CLOSURE TEMPLATE):
CLIENT SIDE TEMPLATING IN MINDTALK
CSS CLASS MAPPING
CSS CLASS MAPPING




     BEFORE
CSS CLASS MAPPING




     AFTER
Closure Template
             +
    Closure Stylesheet
             +
     Closure Compiler
             =
Highly Optimized Web App
Mindtalk when using CSS Mapping
THANK YOU
Reference
• http://sixrevisions.com/graphics-design/jpeg-101-a-crash-course-guide-on-
 jpeg/

• http://www.slideshare.net/bonoseo/web-performance-optimization-wpo

• http://nodejs.org/

• http://getcomparisons.com/node-js-vs-php

• http://blog.pandaform.com/techie/browser-side-template-engines-roundup/

• http://www.agafix.org

Web Optimization Level: Paranoid