snappy apps for cordova by varun vachhar & yuri takhteyev

38
Snappy Apps for Cordova Designer/Developer Varun Vachhar Designer/Developer Yuri Takhteyev Some rights reserved - Creative Commons 2.0 by-sa, image credits on last slide.

Upload: rangleio

Post on 10-Jul-2015

183 views

Category:

Software


0 download

DESCRIPTION

rangle.io's CTO Yuri Takhteyev and Designer/Developer Varun Vachhar looked at a range of techniques for making Cordova apps more "snappy", in particular on older devices. This will include both micro- and macro- optimizations. Bios: Yuri Takhteyev (@qaramazov) is CTO of rangle.io, a former faculty member at the University of Toronto, and a published author, whose interests span software architecture and global software development culture. He is an expert in front-end and test-driven development, server-side development, UX and design. Varun Vachhar (@winkerVSbecks) is a design leader and front-end developer at rangle.io, creating amazing user experiences for mobile and web applications. He specializes in bridging the gap between design and development.

TRANSCRIPT

Page 1: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Snappy Apps for Cordova

Designer/Developer

Varun VachharDesigner/Developer

Yuri Takhteyev

Some rights reserved - Creative Commons 2.0 by-sa, image credits on last slide.

Page 2: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Basic Flow of the Rendering Engine

Parse HTML To Construct

The DOM Tree

Render Tree Construction

Layout of The Render Tree

Painting The Render Tree

http://www.html5rocks.com/en/tutorials/internals/howbrowserswork

Page 3: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

60 fpsSmooth, high frame rates drive user engagement and can affect how much users interact with your website or app

Page 4: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Each frame the browser will:

• Evaluate some JavaScript

• Re-calculate style for the elements

• Re-calculate layout (if styles are modified by JavaScript)

• Draw a subset of the page to various layers

• Then it will use the GPU to composite these layers to the screen

16ms per Frame

Page 5: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

For the most part we’ll be trying to get to 60fps in our work, and that normally means understanding what the browser’s doing and optimizing for it.

Paul Lewis

Page 6: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Re-FlowWhen the changes affect:

• document content • structure • element position

A reflow (or re-layout) happens.

Page 7: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Styles That Affect Layoutheightmargin border-width topfont-sizetext-align font-weight leftline-heightright white-spacemin-height

width paddingdisplayborder position float overflow-yoverflowfont-familyvertical-align clear bottom

Page 8: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Avoid animating properties that trigger layout

Page 9: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Re-Paint

The process by which the browser converts the DOM elements with all their properties into actual pixels to draw.

Page 10: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Styles That Affect Paintcolorvisibility text-decoration background-position outline-color outline-styleoutline-width background-size

border-style backgroundbackground-imagebackground-repeat outline border-radius box-shadow

Page 11: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• The affected element are repainted.

• The layers they belong to are uploaded to the GPU.

• On mobile, the bandwidth between the CPU and GPU is limited, so texture uploads take a long time.

• Therefore, painting takes longer and is particularly expensive.

Animating Paint Properties

Page 12: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Can I Actually Animate Anything?

Page 13: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

You can change these properties without triggering layout or paint.

The Easy Wins! Position transform: translate(npx, npx);

Scale transform: scale(n);

Rotation transform: rotate(ndeg);

Skew transform: skew(X|Y)(ndeg);

Matrix transform: matrix(3d)(…);

Opacity opacity: 0…1;

(The elements will need to be on it’s own composite layer)

Page 14: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

The process where each of the individual textures uploaded to the GPU is drawn out one by one or even in parallel

GPU Compositing

translateZ(0)

translate3d(0,0,0)

will-change

Page 15: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

The Null Transform Hack• Creates a new layer with its own

backing surface

• i.e., the DOM element gets mapped as a texture residing on the GPU

• The content doesn’t change and therefore, the browser needs to build the texture only once

• No re-paint!

Page 16: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Minimizing Repaint

CPU Repaint

GPU Repaint

Page 17: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Should be used sparingly

• Overdoing or having too many layers can cause jank

• Especially on Mobile devices (they have limited VRAM)

¡Caution!

http://wesleyhales.com/blog/2013/10/26/Jank-Busting-Apples-Home-Page

Page 18: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Tools, Not Rules

Page 19: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Profiling With Chrome• Open your app in Incognito mode

• Open the Developer Tools

• Go to the timeline tab

Page 20: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Record

Page 21: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

The Timeline

Page 22: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Layout Thrashing

LoadingScriptingRenderingPainting

30 FPS

60 FPS

Page 23: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Filter

Page 24: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Long Paint

Page 25: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Layout Thrashing

Page 26: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Paint Rectangles

Page 27: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Declarative animations are preferable because the browser can optimize them ahead of time.

Imperative (JS) vs Declarative (CSS)

< ? >

Page 28: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Don't change individual styles one by one.

• Change the class names not the styles

• For dynamic styles, edit the cssText property

// badel.style.left = left + "px";el.style.top = top + "px";

// betterel.className += " theClassName";

// or when calc. dynamicallyel.style.cssText += "; left: " + left + "px; top: " + top + "px;";

Avoid Layout Thrashing

Page 29: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Schedules a function to be executed at the next frame

• Similar to setTimeout(fn, 0)

• All our DOM writes to run together in the next frame.

// Readvar h1 = element1.clientHeight;

// WriterequestAnimationFrame(function() { element1.style.height = (h1 * 2) + 'px';});

// Readvar h2 = element2.clientHeight;

// WriterequestAnimationFrame(function() { element2.style.height = (h2 * 2) + ‘px';});

requestAnimationFrame

Page 30: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Infinite Scroll on an Older Phone

Page 31: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Infinite scroll

• Older Android phones (~Galaxy Nexus)

• Keeping it fast

The Challenge

Page 32: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Waiting at the bottom

• Occasional non-responsiveness

• Slow scrolling

A Few Kinds of Slow

Page 33: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• A fetch queue

• The cost of rendering

• collection-repeat

• Removing Angular

Avoiding the Wait

Page 34: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• An asynchronous rendering queue (using Handlebar)

• Raw DOM insertion

• Batching insertion

• requestAnimationFrame()

• Implemented as a “skinny” directive (most work offloaded to services)

• Downside: a lot more work than using Angular!

Doing it Without Angular

Page 35: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Faster scrolling helps accumulate content

• With more content, scrolling gets slower

• Solution: removing hidden batches (kudos to LinkedIn)

Weight of Accumulated DOM

Page 36: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

• Difficulty figuring out when slowness returned

• ion-scroll vs overflow scrolling

The Return of Slowness

Page 37: Snappy Apps for Cordova by Varun Vachhar & Yuri Takhteyev

Designer/DeveloperVarun Vachhar

THANK YOU!

@winkerVSbecks

winkerVSbecks

CTO, rangle.ioYuri Takhteyev

@qaramazov

yuri