High Performance Mobile Web




             Morgan Cheng
             Feb 17th, 2012
It is Mobile
Mobile Web   Mobile App
The Middle Man




User               Developer
Multiple Platforms
Mobile Web: Cross Platform Solution
Modern Mobile is Blessed with HTML5
Architecture




http://blog.sc5.fi/2012/01/html5-web-app-architecture-explained/
These Best Practices Still Work?




    http://developer.yahoo.com/performance/rules.html
More Tips
Deal With Designer
In the Eyes of Designer
                                         Beautiful
                                         Radial
                                         Gradient


Smooth
Rotating




                                     Stereovision
                                     created by
                                     shadow
In the Eyes of Developer
                               The Background
                               Image Might
                               Hurt
                               Performance
What’s the
rotating
center, radius
and angle?



                                 Should The
                                 Shadow Be
                                 CSS or Image?
Balance For Perfect
Cache is Limited
http://www.stevesouders.com/blog/2010/07/12/mobile-cache-file-sizes/
Always Minify Your Resources
Cookie + LocalStorage
More HTTP Requests Mean Slower
First Visit

               Without Cookie

 Server                              Browser         Resources
                                                                 LocalStorage
              Big HTML




With inline JS, CSS        Extract Resources and Save to
and data-URI               LocalStorage; Set Cookie
Second Visit

               With Cookie

Server                                Browser            Resources
                                                                     LocalStorage
              Big HTML




With reference to            Load Resources and Insert
keys in LocalStorage         into DOM
Favor transition over timer
Animation with Timer

Change Element’s Style with setTimeout
Browser Has No Idea What is Actually Doing

http://jsfiddle.net/r5mYr/
Animation with Transition

Let Browser Know It is Animation
http://jsfiddle.net/Rapvs/
Animation with Transition

Let Browser Know It is Animation
http://jsfiddle.net/Rapvs/

With transform, it is better
http://jsfiddle.net/DwtCc/
Be Careful With CSS3
-webkit-gradient
-webkit-box-shadow



Static Images Render Faster
Background Image + ARIA
background-image Faster Than <img>
<imgsrc=“photo.png” alt=“One Photo”>



<div id=“photo”></div>
#photo {
background-image: url(photo.png);
}
background-image only
 break the semantics
background-image Faster Than <img>

<div id=“photo” role=“img” aria-label=“One
  Photo”></div>
#photo {
background-image: url(photo.png);
}
Reduce Reflow & Repaint
HTML        DOM Tree            Flow




JavaScript                 Render Tree




             Style Sheet
                                  Paint
-webkit-tap-highlight-color:
Overrides the highlight color shown when the
 user taps a link or a JavaScript clickable
 element in Safari on iOS.
/* Avoid Repaint of JS Clickable Element*/

-webkit-tap-highlight-color: transparent
Use Touch Events
touchend Faster Than click
vartapEvent= ‘ontouchend’ in document ?
  ‘touchend’ : ‘click’;

node.delegate(tapEvent, …);
Hardware Acceleration
http://www.sencha.com/blog/understanding-hardware-acceleration-on-mobile-browsers/
/* Force 3D Acceleration */

-webkit-transform: translate3d(0,0,0);

-webkit-transform: tranlsateZ(0);

-webkit-transform: scale3d(1,1,1);

-webkit-transform: scale3dZ(1);
Hardware Acceleration
Consumes Battery & Memory
AppCache For Full Screen
Ideally, Mobile
 Web Can Be
  Full Screen
$(window).on('load', function() {
    /mobile/i.test(navigator.userAgent) &&
    !location.hash&&
setTimeout(function () {
       if (!win.pageYOffset) window.scrollTo(0, 1);
    }, 200);
});
Address Bar is
Hidden




 ToolBar is Still
 There
<!-- enable full-screen mode -->
<meta name="apple-mobile-web-app-capable"
  content="yes"/>

<!-- controls the appearance of the status bar in
  full-screen mode -->
<meta name="apple-mobile-web-app-status-
  bar-style" content="black"/>
Status Bar is
Customized




 ToolBar is Hidden
Till iOS5, Full Screen Web Launched
From Home Screen Cannot Leverage
            Browser Cache
AppCache Helps Full Screen
<!doctype html>
<html manifest=“index.appcache”>
<head>
…


                      CACHE MANIFEST
                      x.html
                      xxx.js
                      xxx.css
                      xxxxxx.png
Dawn of Mobile Web Performance
Thank You!
Find Me

@程墨Morgan

@morgancheng


http://www.facebook.com/m
       organ.chengmo

High Performance Mobile Web