Frontend System Design · Lesson 8 of 86 · 5 min
Reflows and Repaints Explained
This is a classic browser performance question. Your answer shows if you can write efficient animations and DOM updates, or if you'll accidentally ship jank.
When you change a style or modify the DOM, the browser might have to do a surprising amount of work. Knowing when that work happens, and how expensive it is, is the difference between a smooth UI and a laggy one.
The browser's work falls into two main categories: reflow and repaint. A reflow, also called layout, is the calculation of the position and geometry of elements. A repaint, or paint, is the process of filling in the pixels for those elements.
A reflow is expensive. Changing the geometry of one element can affect all the others, forcing the browser to recalculate the layout for a large part of the page. A repaint is less expensive, but it's still work that can block the main thread.
The most important rule is this: a reflow will always trigger a repaint. You can't change an element's size or position without also redrawing it. But a repaint can happen without a reflow.
What triggers a reflow vs. a repaint
Any change that affects an element's geometry triggers a reflow. This includes properties like width, height, margin, padding, border, font-size, top, left, and position. It also happens when you add or remove elements from the DOM, or resize the browser window.
A repaint is triggered by changes to non-geometric, visual properties. Think background-color, color, box-shadow, or outline. These don't change the layout, so the browser can skip the reflow step and just redraw the affected area.
The browser is smart and tries to batch changes, but reading certain properties from the DOM forces it to stop and give you a precise, up-to-the-millisecond value. This means it has to apply all pending style changes and perform a synchronous reflow. Reading properties like offsetHeight, offsetWidth, scrollTop, or calling getComputedStyle() or getBoundingClientRect() are common culprits.
How to spot and fix layout thrashing
The trap: Reading and writing layout-critical properties in a loop. This is the single most common performance mistake related to reflows, and it's called "layout thrashing" or "forced synchronous layout".
When you write a style (el.style.width = '100px') and then immediately read a geometric property (el.offsetWidth), you force the browser to perform a reflow right then and there. Do this in a loop, and you're triggering a separate, synchronous reflow for every single iteration.
In an interview, if you're asked to optimize a piece of JavaScript that manipulates the DOM, layout thrashing is the first thing you should look for. It's a clear signal you understand browser rendering performance.
Here's what it looks like in practice. This code reads the width of each element to make it wider, causing one reflow per element.
// BAD: Layout thrashing
// This code forces a synchronous reflow inside the loop.
function resizeBoxesBad() {
const boxes = document.querySelectorAll('.box');
boxes.forEach(box => {
// WRITE: Invalidate layout
box.style.padding = '10px';
// READ: Force synchronous reflow to get an accurate value
const currentWidth = box.offsetWidth;
// WRITE: Use the read value to trigger another invalidation
box.style.width = (currentWidth * 1.1) + 'px';
});
}
// GOOD: Batching reads and writes
// This code separates reads from writes, allowing the browser to optimize.
function resizeBoxesGood() {
const boxes = document.querySelectorAll('.box');
const newWidths = [];
// 1. First, perform all READS.
// The browser can return cached values without a reflow.
boxes.forEach(box => {
newWidths.push(box.offsetWidth * 1.1);
});
// 2. Then, perform all WRITES.
// The browser batches these changes and performs a single reflow/repaint.
boxes.forEach((box, i) => {
box.style.padding = '10px';
box.style.width = newWidths[i] + 'px';
});
}
The fix is simple: batch your operations. Perform all your reads first, then perform all your writes. This allows the browser's own optimizations to kick in, batching the style invalidations into a single reflow and repaint at the end of the frame.
How to batch DOM changes
Layout thrashing isn't the only problem. Modifying the live DOM tree repeatedly is also slow. Adding 100 elements to a list one by one can trigger 100 reflows.
The solution is to make your changes off-screen.
Use a DocumentFragment. It's a lightweight, in-memory version of a DOM tree. You can build up a complex structure inside a fragment, and then append the entire fragment to the live DOM in a single operation. This results in just one reflow.
Another common technique is to apply multiple style changes at once by toggling a CSS class. Instead of setting el.style.width, el.style.height, and el.style.color individually, define an .active class in your stylesheet and simply use el.classList.add('active'). This groups all layout and paint changes into one browser operation.
Why you should animate transform and opacity
For animations, the goal is to hit 60 frames per second. That gives you a budget of about 16.7 milliseconds per frame to do everything: run JavaScript, calculate styles, perform a reflow, and repaint. A single reflow on a complex page can easily blow that budget, causing dropped frames and a janky animation.
Never animate properties that trigger reflow. Animating top, left, or margin is a common mistake. On every frame of the animation, you're forcing the browser to recalculate the layout of the page.
Instead, use transform and opacity. These properties are special because the browser can often optimize them by handing them off to the compositor thread, and sometimes directly to the GPU. An element being animated with transform can be promoted to its own "compositor layer." Moving this layer doesn't affect the geometry of other layers, so no reflow is needed. The browser just has to re-composite the layers, which is incredibly fast.
This is why transform: translateX(100px) is dramatically more performant than left: 100px.
You can hint to the browser that an element will be animated by using the CSS property will-change: transform. This encourages the browser to create a separate compositor layer for the element ahead of time. Use it sparingly, as creating layers consumes memory and isn't a magic bullet.
Try it
Open the Performance tab in your browser's DevTools. Record a trace of a simple JavaScript animation that moves a <div> across the screen using style.left. Then, do it again using style.transform = 'translateX(...)'. Compare the flame charts. Look for the purple "Layout" events in the first trace and notice their absence in the second.
Next: We'll look at how the browser schedules these tasks with the Event Loop: Call Stack, Task and Microtask Queues.
Lock it in — apply this lesson under interview conditions.