Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
08

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.

Try a free mock interviewPractice system design problems
Previous

Layout, Paint and Compositing

Next

Event Loop: Call Stack, Task and Microtask Queues