Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Handbooks  /  Browser Internals
Handbook~16 min readFrontendworked math + runnable code
The Browser Internals Handbook

From tags
to pixels.

You ship HTML and CSS — a tree of tags and a pile of style rules — and somehow a browser turns it into a living, scrolling, animating picture at 60 frames a second. It does this through a render pipeline: parse, style, lay out, paint, composite. Most of what makes a page fast or janky comes down to one expensive stage — layout, the geometry math — and how often your code forces the browser to redo it. This handbook is that pipeline, why reflow costs what it costs, and the small rules that keep a page smooth.

01

The render pipeline

A browser turns your markup into pixels through a fixed sequence of stages called the render pipeline (or critical rendering path). It parses HTML into the DOM tree and CSS into the CSSOM, combines them into a render tree of everything visible, then runs layout to compute the exact position and size of every box, paint to fill in the pixels (text, colors, borders, shadows), and finally composite to stack the painted layers into the image you see.

The crucial fact is that the stages are ordered and dependent: each one feeds the next, so a change early in the pipeline forces everything downstream to run again. Change the text content and you may re-run layout, paint, and composite. Change only a color and you can skip layout and just repaint. Change only a transform and you can skip layout and paint, going straight to composite. That dependency chain is the entire performance story: the cost of a visual update is decided by how far back in the pipeline your change reaches. And the stage you most want to avoid re-running is layout.

The one-sentence version

The render pipeline is parse → style → layout → paint → composite, each stage depending on the last — so the cost of an update is how far back it reaches, and layout (reflow) is the expensive stage to avoid re-triggering.

02

Reflow is the tax

Layout — also called reflow — is where the browser computes the geometry of every element: where each box sits and how big it is. It's the pipeline's most expensive stage, and the reason is interdependence. Boxes affect each other: make one element wider and it can push its siblings, stretch its parent, and shift all their descendants. So a single geometric change can force the browser to recompute a whole subtree, at a cost that grows with the number of nodes it has to lay out.

Anything that touches geometry triggers a reflow: changing width, height, margin, padding, top/left, font-size, or adding/removing a DOM node. Changes that only affect appearance — color, background, visibility — skip layout and only repaint, which is much cheaper. This split is the single most useful thing to internalize about browser performance: geometric changes are expensive because they cascade; cosmetic changes are cheap because they don't. Every front-end optimization about "smooth scrolling" and "jank-free animation" is, at bottom, about not paying the reflow tax more often than you must.

03

Layout thrashing

The classic performance killer is layout thrashing: forcing the browser to reflow many times in a row when one would do. The browser is actually smart — it tries to batch your DOM changes and reflow once, at the end of the frame. You defeat that batching by interleaving writes and reads.

Batched vs thrashing (3 geometric writes)
BatchedWrite width, height, left → browser coalesces → 1 reflow at frame end.
Thrashingwrite → read offsetWidth → write → read → … forces a sync reflow each time → 3 reflows.
WhyReading a layout property needs an up-to-date answer → browser must flush a reflow now.
FixRead all layout values first, then do all writes → one reflow.

The trap is subtle because reading looks harmless. But when you read a layout property like offsetHeight after having written a geometric change, the browser can't give you a stale number — it must synchronously flush a reflow to compute the current value. Do that inside a loop (write, read, write, read…) and you trigger a reflow per iteration instead of one at the end. The fix is a discipline: batch reads and writes separately — gather all the measurements you need first, then apply all the mutations — so the browser coalesces everything into a single layout pass. Batching geometric updates is one of the highest-leverage optimizations in front-end code, and it's the direct payoff of understanding reflow.

04

The reflow math

Only geometric property changes force a reflow; a reflow's cost scales with the number of nodes it must lay out. Batched, many geometric writes collapse to one reflow; unbatched, each triggers its own:

reflows  =  1 if batched else (#geometric changes)     work  =  reflows × node_count

3 geometric writes over 100 nodes: batched = 1×100 = 100 units; thrashing = 3×100 = 300. Paint-only changes (color, background) = 0 reflows.

So the cost of a visual change depends entirely on which stage it reaches and how many times you re-enter it:

geometry (width, height, top…) → reflow     appearance (color, visibility) → repaint only     transform/opacity → composite only

The further right, the cheaper. The runnable version below classifies properties and computes reflow counts and total layout work, batched vs not.

RUN IT YOURSELF

Counting reflows

Browser performance comes down to the render pipeline. Changing a geometric property (width, height, position) forces a reflow — the browser recomputes layout for the affected nodes, at a cost that grows with node count. Changing a paint-only property (color, background) skips layout entirely. And the killer detail: batching geometric writes collapses them into one reflow, while interleaving reads and writes forces one reflow each — layout thrashing. Change the property list, node count, or batching flag and watch the reflow count and total layout work move.

CPython · WebAssembly
05

The fast path

To animate at a smooth 60 frames per second, the browser has ~16 milliseconds per frame to produce a new picture. Run layout and paint every frame and you'll blow that budget and drop frames (jank). The trick is to stay on the cheapest part of the pipeline — compositing — by animating only properties that don't need layout or paint:

What you changePipeline stages re-run · cost
width / height / top / leftLayout → Paint → Composite · expensive (reflow every frame — janky)
color / background / box-shadowPaint → Composite · medium (repaint, no layout)
transform / opacityComposite only · cheap (often GPU, its own layer — smooth 60fps)

So the practical rule for movement is: use transform: translate() instead of top/left, and opacity instead of changing visibility or background — because transform and opacity can be handled by the compositor on a separate layer, frequently on the GPU, without ever re-running layout or paint. That's why two animations that look like they do "the same thing" can feel utterly different: one takes the composite-only fast path and glides; the other reflows every frame and stutters. Combined with batching your reads and writes, staying on the composite path is most of what "web performance" means in practice. Understand the pipeline and the fix for a janky page is usually obvious: figure out which stage you're re-running every frame, and stop.

06

Pitfalls

The first is forced synchronous layout hidden in innocent code — a loop that sets a style and then reads offsetHeight/getBoundingClientRect() right after. Each read forces the browser to flush a reflow to answer accurately, so a loop over N elements can cause N reflows. It's the most common cause of scroll and animation jank, and it's invisible unless you know to look. The fix is the batching discipline: read all layout values in one pass, then write all mutations in the next.

Two more. Animating layout properties: transitioning width or left triggers reflow every frame and will never be smooth on a complex page — reach for transform instead, always. And giant DOM trees: since reflow cost grows with the number of nodes involved, a page with tens of thousands of DOM nodes reflows slowly no matter how careful you are; virtualize long lists (render only what's on screen) so layout works on a small tree. The unifying idea is that the browser is a pipeline with one expensive stage, and your job is to reach that stage as rarely as possible: prefer cosmetic changes to geometric ones, batch the geometric ones you can't avoid, and animate on the compositor. Learn the pipeline and web performance stops being folklore — it becomes a short, mechanical checklist about which stage a change re-runs and how often.

Worth knowing

Batch reads then writes to avoid forced synchronous layout, animate transform/opacity (composite-only) instead of width/top (reflow), and virtualize huge lists so layout works on a small tree. The whole game is re-running the layout stage as rarely as possible.

By the numbers

Worked example: one janky update, traced

Say a page has 2,000 DOM nodes and one full reflow of it takes 2 ms. That figure is illustrative; read your own from the Layout entries in the DevTools Performance panel. Use the page’s linear model, cost = reflows × nodes, to trace a card-resizing loop, a sliding panel and a long list.

StepWhat the code doesArithmeticResult
1Frame budget at 60 Hz1000 ÷ 6016.7 ms
2Loop over 20 cards: set height, then read offsetHeight20 forced reflows × 2 ms40 ms
3Does it fit the frame?40 ÷ 16.72.4 budgets: about 2 frames dropped
4Same loop, all reads first, then all writes1 reflow × 2 ms2 ms, fits easily
5Slide a panel for 1 s by animating left60 frames × 1 reflow × 2 ms120 ms of layout, plus a repaint every frame
6Same slide with transform: translateX()no layout, no paintcomposite only
7A 10,000-row list, 5 nodes per row50,000 nodes × (2 ms ÷ 2,000 nodes)50 ms per reflow
8Virtualized to 30 rendered rows150 nodes × (2 ms ÷ 2,000 nodes)0.15 ms per reflow

Steps 2 to 4 are layout thrashing in numbers. Nothing changed except the order of reads and writes, and the cost fell twentyfold. Step 5 can look fine on a fast laptop, where 2 ms a frame fits. The same page on a slower phone does more work per node, and the slide starts to stutter. Step 6 avoids the question, because the work it skips is the work that varies by device. Steps 7 and 8 explain why virtualization is not optional for long lists: no amount of batching makes 50 ms fit a 16.7 ms frame.

Trade-offs

Cost and trade-offs: what each kind of update costs

Using the page’s model. Time is the layout and paint work per update. Space is the memory or extra structure it needs.

CaseTimeSpaceWhy
N interleaved writes and layout readsN reflows × nodesNoneEach read after a write forces a synchronous flush. This is the worst case, and it hides in loops.
Batched: all reads, then all writes1 reflow × nodesAn array of N measurementsThe browser coalesces the writes into its normal end-of-frame layout.
Paint-only change (color, background)0 reflows; repaint of the changed areaNoneCheaper, but a large repainted area every frame still costs.
transform / opacity on its own layerComposite onlyA layer texture of about width × height × 4 bytes: 3.2 MB for 1000 × 800, 12.8 MB at 2× pixel densityWhy will-change on everything backfires: layers cost memory, and too many slow the compositor.
Virtualized listReflow over visible rows onlyScroll position and row-height bookkeeping in JSLayout work stops depending on list length. Find-in-page and anchor links to unrendered rows stop working.
contain: layout, content-visibility: autoLayout bounded to the subtree, or skipped while off screenNoneYou promise the browser a box cannot affect the outside, so a change inside it does not reflow the page.

The trade-off runs down the table. Each cheaper row asks you for something: an order discipline, a layer’s memory, JS bookkeeping, or a promise about containment. The expensive first row asks for nothing, which is why it is the default.

In the wild

Where this shows up in production and interviews

  • DevTools tells you directly

    In Chrome’s Performance panel, layout shows up as purple Layout entries. A layout forced from script is flagged as a forced reflow, with a link to the line of JS that read the geometry. Record one janky interaction and you usually find the read that caused it.

  • Frameworks batch your writes

    React batches state updates into one render and commit, and Angular applies a change-detection pass in one go. That covers the write side. The read side is still yours: a getBoundingClientRect() inside an effect that runs after another component’s write still forces a reflow.

  • Core Web Vitals

    Two of Google’s page-experience metrics are this pipeline in disguise. CLS counts unexpected layout shifts, such as an image without dimensions pushing text down. INP measures how long the page takes to paint after an input, so a forced reflow inside a click handler adds to it directly.

  • The interview version

    “Why is this animation janky?” and “what happens after the HTML arrives?” are both answered by the pipeline. Name the stage the change re-runs, how often it runs it, and the fix: batch reads before writes, move animation to transform, virtualize the list. Numbers like the 16.7 ms budget make the answer concrete.

Self-check

Check yourself

A loop runs el.style.width = w; total += el.offsetHeight over 50 elements. How many reflows, and what is the fix?

Up to 50. Every offsetHeight read after a width write forces a synchronous layout. Split it into two loops, one that writes all 50 widths and one that reads all 50 heights, and the browser lays out once.

You hide an element with display: none, and later with visibility: hidden. Which one triggers a reflow?

display: none. It removes the box from layout, so its siblings move. visibility: hidden keeps the box and its space and only stops drawing it, so it needs a repaint, not a reflow.

An update spends 5 ms in layout and fits easily at 60 Hz. What happens on a 120 Hz display?

The budget halves to 1000 ÷ 120 ≈ 8.3 ms, so the same 5 ms of layout now uses about 60% of the frame before any script or paint runs. Work that was invisible at 60 Hz can drop frames at 120 Hz.

To slide in a menu, a teammate adds will-change: transform to every element on the page “for speed”. Good idea?

No. Each promoted element can get its own layer, roughly width × height × 4 bytes of memory, and many layers slow compositing. Promote only the element that animates, ideally just for the length of the animation.

Frequently asked

Quick answers

How does a browser render a page?

Parse HTML→DOM and CSS→CSSOM, build a render tree, then layout (geometry) → paint (pixels) → composite (stack layers). Each stage feeds the next.

Why is reflow expensive?

Layout is interdependent — one box's size shifts others — so a geometric change can recompute a whole subtree, at cost growing with node count.

What is layout thrashing?

Interleaving DOM writes and layout reads forces a synchronous reflow each time. Fix: batch all reads, then all writes → one reflow.

How do you animate smoothly?

Animate transform and opacity — the compositor handles them (often on GPU) without layout or paint, so they hold 60fps.

▶  Watch it explained

How a browser turns HTML into pixels (the 5-stage render pipeline)

Finished this one? 0 / 209 Handbooks done
NextThe Git Internals Handbook Next on the “How computers actually work” ladder · rung 7 of 7

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.