Home

Awesome

Browser Rendering Optimization

Notes on browser rendering optimization and hitting 60fps smoothness!

Before understanding how to optimise web sites and applications for efficient rendering, it’s important to understand what is actually going on in the browser between the code you write and the pixels being drawn to screen.

There are six different tasks a browser performs to accomplish all this:

  1. Downloading and parsing HTML, CSS and JavaScript
  2. Evaluating JavaScript
  3. Calculating styles for elements
  4. Laying out elements on the page
  5. Painting the actual pixels of elements

Sometimes you may hear the term "rasterize" used in conjunction with paint. This is because painting is actually two tasks: 1) creating a list of draw calls, and 2) filling in the pixels. The latter is called "rasterization" and so whenever you see paint records in DevTools, you should think of it as including rasterization.

Putting the user in the center of performance

Browser rendering pipeline - 60fps

60fps = 16ms/frame, but you actually get only around 10-12ms to do all your work due to browser overhead.

App Life Cycles (RAIL)

RAIL

Actual Chronological Order

  1. Load (~1 sec) Initial page load. Download and render your critical resources here.
  2. Idle (~ 50ms chunks) Do all non-essential work to ensure interactions that occur later on feel instantaneous. eg. lazy load items, do pre-animation calcs etc.
  3. Response (~100ms) On interaction, respond within 100ms.
  4. Animations (~16ms) In reality we get ~12ms since the browser has some overhead.

RAIL Time Table

What happens during style changes?

For example, during an opacity change or transform animation, only the composite is triggered.

For JavaScript code to run faster

Animation

Webworkers

Web Worker

Styles and Layout (Recalc Styles)

Repaints and Reflows

Painting is the process by which the browser takes its abstract collection of elements with all their properties, and actually calculates the pixels to draw. This includes calculating styles such as box shadows and gradients, as well as resizing images. A repaint occurs when changes are made to an elements skin that changes visibility, but do not affect its layout. Examples of this include outline, visibility, or background color. According to Opera, repaint is expensive because the browser must verify the visibility of all other nodes in the DOM tree.

A reflow is even more critical to performance because it involves changes that affect the layout of a portion of the page (or the whole page). Reflow of an element causes the subsequent reflow of all child and ancestor elements as well as any elements following it in the DOM. According to Opera, most reflows essentially cause the page to be re-rendered.

So, if they’re so awful for performance, what causes a reflow?

Unfortunately, lots of things. Among them some which are particularly relevant when writing CSS:

For the complete list check Paul Irish's gist

Compositing and Painting

Thanks to Paul Lewis and Cameron Pittman for their course on Udacity, which presented deeper insights to this topic.

Other Links:

Introducing RAIL: A User-Centric Model For Performance

The RAIL Performance model