← How the browser works

Parse, style, layout, paint, composite in one minute, and the other Shorts on How the browser works

How the browser works · 1-min Short

URL to pixels

Why the agent's tiny page stays blank on a phone.

Start the lesson
transcript

Your page's files are tiny. The wait is round trips, not downloads.

First the browser needs the server's address. It asks DNS, the internet's directory, and waits for the answer. That is one round trip, unless the address is already cached.

With the address, the browser opens a TCP connection. Both sides exchange a handshake before any data is sent. That is one more round trip.

The connection is then secured with TLS. In TLS 1.3 the two sides agree on keys in one round trip. Only now can the browser ask for the page.

The request goes out and the first byte of HTML comes back. That makes four round trips. On a fast connection Chrome measured under a tenth of a second. At a phone's 150 millisecond round trip, the same four take 600.

The font stylesheet names a fifth server, the one holding the font file. What does the browser do next?

It is a new origin, found late. Its lookup and handshakes only start once the font stylesheet is in, so the font arrives last, after the page has already painted.

How the browser works · 1-min Short

Critical rendering path

Why one script tag keeps the whole page white.

Start the lesson
transcript

One script tag in the head can keep your whole page white.

First, the page as it was before the widget. The parser reads the HTML one token at a time and builds the DOM, a tree of every element.

The link tag names a stylesheet. Its rules become the CSSOM, a second tree. Until it is ready, the browser will not paint: CSS blocks rendering.

The DOM and the CSSOM join into the render tree, which holds only what will be drawn. Then layout and paint. Chrome measured first pixels well under a tenth of a second.

Now the agent's widget goes in the head as a plain script tag, and its server answers one second late. When does the heading appear?

The parser reaches the script and stops. It cannot read another tag until the script has arrived and run. The CSS is ready, but there is no body to paint until then.

Add the defer attribute. The script still downloads, but the parser carries on, and the script runs only after parsing ends, just before DOMContentLoaded. First pixels are back, well under a tenth of a second.

How the browser works · 1-min Short

Parse, style, layout, paint, composite

Why moving a drawer with left stutters and transform does not.

Start the lesson
transcript

Animate with left, and the browser redoes layout every frame.

Each frame starts with style. The browser works out which CSS rules apply to each element and calculates its final styles.

Then layout. With the styles known, the browser calculates how big each box is and where it sits on the page.

Then paint. The browser fills in the pixels: text, colours, borders, shadows. It paints onto layers, here one for the page and one for the drawer.

Last, composite. The compositor puts the layers together in the right order and sends the frame to the screen. It runs on its own thread, apart from your JavaScript.

At sixty frames a second, all of this has about sixteen milliseconds. Now the drawer moves with transform instead of left. Which stages run?

Chrome measured both, with the processor slowed four times, like a phone. With left, layout ran 119 times and paint 236. With transform, both ran only at page load. Style still runs, because the script sets a style every frame.

How the browser works · 1-min Short

Reflow vs repaint

Why reading offsetHeight in a loop froze a grid of cards.

Start the lesson
transcript

Read offsetHeight in a loop, and layout runs once per card.

Setting a style is a write. The browser does not lay out right away. It only marks layout as out of date, and the script keeps going.

When the script's task ends, the browser lays out once for all the writes, then paints and composites. This is the cheap, normal path.

Reading offsetHeight is different. To give a correct answer, the browser must lay out right now, in the middle of the script. This is a forced synchronous layout.

In the agent's loop, every write is followed by a read, so every read forces another layout. Chrome measured one call: layout ran 301 times, and the task took 191.9 milliseconds. One frame is only 16.7 milliseconds.

Now change the order. First write every height, then read every height, then write the result. How many layouts?

The first read forces one layout, and the other reads reuse it. The final writes are laid out once at the end. Measured: 2 layouts, and the task took 9.7 milliseconds instead of 191.9.