Frontend System Design · Lesson 7 of 86 · 4 min
Layout, Paint and Compositing
This is how you explain why one animation is janky and another is smooth, and prove you can write performant UI code.
After the browser builds the render tree, it has to turn that tree into pixels on the screen. This happens in a sequence called the rendering pipeline. Understanding this sequence is non-negotiable for diagnosing and fixing UI performance issues.
The pipeline has three main stages that your code can affect: Layout, Paint, and Compositing. The goal is to do as little work as possible. The cheapest updates only involve Compositing. The most expensive ones start all the way back at Layout.
The pipeline: from CSS change to pixels
When you change a style, the browser has to figure out which of these stages to run. It does the minimum work required.
-
Layout: The browser calculates the geometry of elements. Where are they on the page? How big are their boxes? A change to one element's width can affect everything after it, so this stage can be expensive. This is also called "Reflow."
-
Paint: The browser fills in the pixels for each element in memory. It draws out the text, colors, images, borders, and shadows. This happens on one or more "layers." This is also called "Repaint."
-
Compositing: The browser draws the layers to the screen in the correct order. Since this is just moving existing layers around, often on the GPU, it's the cheapest stage to run.
This diagram shows how different CSS property changes can skip the expensive stages. This is the key to performant updates.
Your job is to write CSS and JavaScript that triggers the pipeline as far to the right of this diagram as possible.
How your code triggers each stage
You can't optimize what you can't identify. You need to know which properties affect which stage.
Layout is triggered by any property that changes an element's geometry or position. Think width, height, margin, padding, border, top, left, or font-size. Changing a layout property is costly because it can cause a chain reaction, forcing the browser to recalculate the layout for a large part of the document.
Paint is triggered by properties that change an element's appearance without affecting its geometry. Think background-color, color, box-shadow, or outline. Paint is less expensive than Layout because a change to one element doesn't affect the position of others. But it's not free, especially if you're painting a large or complex area.
Compositing is the ideal path. This is what happens when you change properties that the GPU can handle directly, without needing to re-calculate layout or re-paint pixels. The main ones are transform and opacity. The browser just has to move the existing, already-painted element. This is why you always use transform: translateX(10px) for movement animations instead of changing left: 10px.
The interview question: "How would you animate an element moving across the screen smoothly?" Your answer starts with
transformand explains why it's better by referencing this pipeline. You'd explain that animatingleftormargin-lefttriggers Layout on every frame, which is slow, whiletransformonly triggers Compositing.
You can also give the browser a hint that an element will be animated by using will-change: transform, opacity. This encourages the browser to create a separate layer for the element ahead of time.
The trap: Don't overuse will-change. Every composited layer consumes GPU memory and resources. Applying it to too many elements can actually slow your site down. Use it only on elements that have complex, ongoing animations.
The biggest performance trap: Forced Synchronous Layout
Normally, the browser is smart. If you change ten styles in a row with JavaScript, it will batch them and run one single Layout calculation at the end of the task, just before it needs to paint the next frame.
However, you can break this batching. If you change a style and then immediately ask for a geometric value (like element.offsetHeight or element.getBoundingClientRect()), the browser has no choice. It must stop, run Layout synchronously, and give you the updated value.
Doing this in a loop is called "layout thrashing" or "forced synchronous layout," and it's a massive performance killer.
Here is what it looks like in code.
const badBox = document.getElementById('badBox');
const badBtn = document.getElementById('badBtn');
badBtn.addEventListener('click', () => {
console.time('Layout Thrashing');
for (let i = 0; i < 100; i++) {
// WRITE: Change a style that affects layout
badBox.style.width = `${100 + i}px`;
// READ: Immediately read a layout property.
// This forces the browser to perform a synchronous layout
// for each and every iteration of the loop.
console.log(`Current offsetWidth: ${badBox.offsetWidth}`);
}
console.timeEnd('Layout Thrashing');
});
This code forces 100 separate Layout calculations. An optimized version would perform all the reads first, then all the writes, allowing the browser to perform just one Layout at the end.
Try it
Take the "layout thrashing" code example above. Rewrite the click handler to avoid forcing a synchronous layout on every iteration. The goal is to separate the style reads from the style writes so the browser can batch the layout work. You won't be able to log the width on each iteration, and that's the point—the fix involves restructuring the logic to avoid that need.
Next: We'll dig deeper into the two most common performance killers in our lesson on Reflows and Repaints Explained.
Lock it in — apply this lesson under interview conditions.