optimizing javascript runtime performance for...

54
Optimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw

Upload: others

Post on 16-Sep-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Optimizing JavaScript Runtime Performance for Touch

Stephen Woods@ysaw

Page 2: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Native-Like Experience

Page 3: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Hardware

Core OS

Core Services

Media

Cocoa Touch

Page 4: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Hardware

Core OS

Core Services

Media

Cocoa Touch

WebKit

JavaScript Runtime

WebCore

Page 5: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

The Mobile Web is Slow

Page 6: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

What Happens at runtime?

• Fetch, format and Display Data

• Handle UI Events

• Move Pixels

Page 7: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Fetching Data

Page 8: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 9: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 10: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Caching: The cause of, and solution to, all of life's problems

Page 11: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

$.get('data.json',  dataCallback);

Page 12: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

$.get('/data.json',  dataCallback);

myDAL.getData('key',  callback);

Page 13: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

getData  =  function(key,  callback)  {    if(cache[key])  return  cache[key];

   $.get('/data.json',  function(data){        cache[key]  =  data;        callback(data);    }}

myDAL.getData('key',  callback);

Page 14: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

window.localStorage['cache']  =          JSON.stringify(cache);

Page 15: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

TTL

Writes Invalidate Cache

Page 17: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Least Recently Used

Page 18: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 19: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 20: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 21: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 22: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 23: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 24: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Computing the size of the Cache.

JSON.stringify(localStorage).length

Page 25: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Handling Events

Page 26: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Event handling is a conversation

Page 27: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

If a gesture is in progress, the UI must be in motion.

Page 28: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Moving Pixels

Page 29: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

30fps = 33.3ms Per Frame60fps = 16.6ms Per Frame

Page 30: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 31: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Write-Only DOM

Page 32: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

getOffsetHeight : 74% slower than get className

Page 33: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

If you can, cache the value, not the node

Page 34: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 35: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 36: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

var  offsetCache  =  {};

function  getOffsetHeight(id)  {        if(offsetCache[id])  {                return  offsetCache[node.id];        }  else  {                offsetCache[id]  =                          document.getElementById(id).offsetHeight        }}

Page 37: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

this.nodePositionMap[thisId]  =  {    top:  thisNode.getY(),    height:  thisNode.get('offsetHeight'),    defer:photoList[i].defer  ?  {img_url:  'bar'}:  false};

Page 38: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 39: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

background-­‐image:  'foo.jpg';background-­‐size:  100%;transform:  translate3d(10,10,1);

Page 40: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 41: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 42: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

User feedback comes first

Page 43: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

isSwiping = 1;

Page 44: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

function  handleXHR(resp)  {    if(isSwiping)  {        setTimeout(function(){                handleXHR(resp);        },20);    }    processData(desp);}

Page 45: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience
Page 46: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

• Load image ~ 300ms (async)

• Find Faces ~ 100ms (sync)

• Compute Entropy Crop ~ 30ms (sync)

• Smooth Animation (sync)

Page 47: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Run Animation in a Separate Thread

Page 48: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

slide1.style.transition  =  transitionDuration  +                                                      's  transform  ease-­‐out';slide1.style.transform  =  computedEndFrame;

Page 49: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Run Slow Code in a Worker*

Page 50: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

Run Slow Code in a Worker*

*except on Android Browser

Page 51: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

worker  =  new  Worker('/smart-­‐crop-­‐worker.js');

Page 52: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

UI Thread Handles Events and Orchestration

Page 53: Optimizing JavaScript Runtime Performance for Touchtopic.it168.com/factory/velocity2013/27.pdfOptimizing JavaScript Runtime Performance for Touch Stephen Woods @ysaw Native-Like Experience

• Cache data where possible

• Update the UI on events, use transforms when possible

• Multitask when you can, both with threads and yeilding