72
Performance beyond Page load CSS Conf Asia 2015

Performance beyond page load - CSS Conf Asia 2015

Embed Size (px)

Citation preview

Page 1: Performance beyond page load - CSS Conf Asia 2015

Performance beyond Page load

CSS Conf Asia 2015

Page 2: Performance beyond page load - CSS Conf Asia 2015

Let’s start with a small survey

Page 3: Performance beyond page load - CSS Conf Asia 2015

How many of you like

animations in

Material Design?

Page 4: Performance beyond page load - CSS Conf Asia 2015

But, you won’t like animations if they’re

NOT smooth

Page 5: Performance beyond page load - CSS Conf Asia 2015

Rendering Performance

Goal: Build smooth interaction experiences

Page 6: Performance beyond page load - CSS Conf Asia 2015

Let’s start with a Case Study

Page 7: Performance beyond page load - CSS Conf Asia 2015

Marriage Proposal

Page 8: Performance beyond page load - CSS Conf Asia 2015

Next thing?

Page 9: Performance beyond page load - CSS Conf Asia 2015

E-Shopping

Page 10: Performance beyond page load - CSS Conf Asia 2015

thoughwhat happened next...

Page 11: Performance beyond page load - CSS Conf Asia 2015

Reaction

Page 12: Performance beyond page load - CSS Conf Asia 2015

Bad performance of Shopping portal

which lead to..

Page 13: Performance beyond page load - CSS Conf Asia 2015
Page 14: Performance beyond page load - CSS Conf Asia 2015

Discussion of the performance impact

followed by actions

Page 15: Performance beyond page load - CSS Conf Asia 2015

Wait

before that we need to understand what’s happeningtechnically, right?

Page 16: Performance beyond page load - CSS Conf Asia 2015

Apoorv Saxena

Full stack Engineer, @apoorv_saxena

India

Page 17: Performance beyond page load - CSS Conf Asia 2015
Page 18: Performance beyond page load - CSS Conf Asia 2015

+ Workshops+ Meetups

Page 19: Performance beyond page load - CSS Conf Asia 2015

Let’s start with Basics

Page 20: Performance beyond page load - CSS Conf Asia 2015

BIOSCOPE

Page 21: Performance beyond page load - CSS Conf Asia 2015

What is smooth interaction?

Devices refresh their screen 60 times a second and even more than that.

Thus, frame budget for smooth interaction is 16ms

(1 second / 60 frames = 16.66 ms / frame)

Though, browser has got some householding work as well, which leaves us with about 10ms in total.

Page 22: Performance beyond page load - CSS Conf Asia 2015

10ms isn’t a lot of time

Page 23: Performance beyond page load - CSS Conf Asia 2015

When failing to meet the frame budget of 10ms, we come across

Jank

Page 24: Performance beyond page load - CSS Conf Asia 2015
Page 25: Performance beyond page load - CSS Conf Asia 2015

Browser rendering process

Page 26: Performance beyond page load - CSS Conf Asia 2015

Pixel Pipeline

Page 27: Performance beyond page load - CSS Conf Asia 2015

Layout

Page 28: Performance beyond page load - CSS Conf Asia 2015
Page 29: Performance beyond page load - CSS Conf Asia 2015

width overflow height

padding font-family margin

display vertical-align border-width

border clear top

position bottom font-size

float left text-align

overflow-y right line-height

font-weight white-space min-height

Page 30: Performance beyond page load - CSS Conf Asia 2015

Paint

Page 31: Performance beyond page load - CSS Conf Asia 2015
Page 32: Performance beyond page load - CSS Conf Asia 2015

Painting

Page 33: Performance beyond page load - CSS Conf Asia 2015

color border-style

visibility background

text-decoration background-image

background-position background-repeat

outline-color outline

outline-style border-radius

background-size box-shadow

Page 34: Performance beyond page load - CSS Conf Asia 2015

Compositing

Page 35: Performance beyond page load - CSS Conf Asia 2015
Page 36: Performance beyond page load - CSS Conf Asia 2015
Page 37: Performance beyond page load - CSS Conf Asia 2015

Performance impact of operations

Layout > Paint > Composite

Page 38: Performance beyond page load - CSS Conf Asia 2015

How to analyse the performance of a

webpage

For use in Development

Page 39: Performance beyond page load - CSS Conf Asia 2015

Chrome Dev Tools: FPS meter

Demo

Page 40: Performance beyond page load - CSS Conf Asia 2015
Page 41: Performance beyond page load - CSS Conf Asia 2015

Chrome Dev Tools:Enable paint flashing

Demo

Page 42: Performance beyond page load - CSS Conf Asia 2015
Page 43: Performance beyond page load - CSS Conf Asia 2015

Chrome Dev Tools: Timeline

Page 44: Performance beyond page load - CSS Conf Asia 2015
Page 45: Performance beyond page load - CSS Conf Asia 2015

Performance best practices

Page 46: Performance beyond page load - CSS Conf Asia 2015

CPU

v

GPU● Not efficient in handling

graphics related operations.

JavaScript utilises CPU followed by GPU when manipulating DOM style.

s

● Optimized for handling Graphics operations.

CSS directly utilises GPU for applying CSS operations and thus hardware accelerated.

Page 47: Performance beyond page load - CSS Conf Asia 2015

Rule of Thumb:

Use classes to update element styling instead of using JavaScript

Page 48: Performance beyond page load - CSS Conf Asia 2015

Using CSS for element’s style manipulation:

var player = $(‘#player’);player.removeClass(‘move--left’);player.addClass(‘move--right’);

Using JS for element’s style manipulation:

var player = $(‘#player’);$(‘#player’).css({ left: ‘0px’, right: ‘10px’ });

Code:

Page 49: Performance beyond page load - CSS Conf Asia 2015

Style Calculations

Reference: CSSTriggers.com

Page 50: Performance beyond page load - CSS Conf Asia 2015

Must follow:

I. Understand what impacts rendering performance (Profile)

II. Choose your CSS rules wisely

III. Avoid triggering layout / paint operation repeatedly or

when animating

IV. Prefer using transform and opacity changes (trigger only

composite operation) for animations

Page 51: Performance beyond page load - CSS Conf Asia 2015

Reduce complexity of CSS selectors

Page 52: Performance beyond page load - CSS Conf Asia 2015

Roughly 50% of the time used to calculate the computed style for an element is used to match selectors, and the other half of the time is used for constructing the RenderStyle (computed style representation) from the matched rules.

Rune Lillesveen, Opera / Style Invalidation in Blink

Page 53: Performance beyond page load - CSS Conf Asia 2015

.title { /* styles */

}

// performant

.box:nth-last-child(-n+1)

.title { /* styles */

}

// less performant

Page 54: Performance beyond page load - CSS Conf Asia 2015

Remove unusedCSS rules

CSS selector parsing is optimized, though unused CSS rules have a bigger performance impact

Automate using Gulp or Grunt tasks

Page 55: Performance beyond page load - CSS Conf Asia 2015

Reduce number of elements being styled

Why to paint the entire body, when changes can/should be limited to that element

Example: Google.com

Page 56: Performance beyond page load - CSS Conf Asia 2015

Measure your style recalculation cost

Page 57: Performance beyond page load - CSS Conf Asia 2015

Possible Jank condition:

If a user hovers over a long list of <li> elements while scrolling

li:hover { background-color: red; /* Layout / Paint operation */

}

Page 58: Performance beyond page load - CSS Conf Asia 2015

Reduce paint areas

Don’t paint unnecessarily. Avoid Union of Damaged regions.

Page 59: Performance beyond page load - CSS Conf Asia 2015
Page 60: Performance beyond page load - CSS Conf Asia 2015

CSS Animations

Use composite CSS operations inside keyframe definition such as transform and avoid triggering paint / layout operation

Page 61: Performance beyond page load - CSS Conf Asia 2015

Debounce input handlers

e.g. disable hover event listener while scrolling

Page 62: Performance beyond page load - CSS Conf Asia 2015

Example

Page 63: Performance beyond page load - CSS Conf Asia 2015

Promote elements that update frequently

Caution: Don’t promote elements without profiling

Page 64: Performance beyond page load - CSS Conf Asia 2015
Page 65: Performance beyond page load - CSS Conf Asia 2015

Did you know?

Fixed elements having “position: fixed” property and scrollable elements, are

repainted by the browsers

Page 66: Performance beyond page load - CSS Conf Asia 2015

Example

.moving-element { will-change: transform;}.moving-element { transform: translateZ(0);}

Page 67: Performance beyond page load - CSS Conf Asia 2015

GIFs and Performance

Disable animation when not inside the viewport

Page 68: Performance beyond page load - CSS Conf Asia 2015
Page 69: Performance beyond page load - CSS Conf Asia 2015

Live Performance Audit

Page 70: Performance beyond page load - CSS Conf Asia 2015

At EdgeConf 2012,

Facebook confirmed this when it mentioned that in an A/B test, it slowed

down scrolling from 60fps to 30fps, causing engagement to collapse.

Page 71: Performance beyond page load - CSS Conf Asia 2015

Path to Performance

http://perfaudit.com, @perfaudit

Page 72: Performance beyond page load - CSS Conf Asia 2015

Thanks.