Repaints And Reflows

Repaints occur when changes are made to an element’s visual properties, such as its color or font size. These changes can be triggered by a variety of events, such as hovering over an element with the mouse or changing the window size. Repaints can be computationally expensive, especially on complex pages with many elements, because the browser must redraw the affected elements and any elements that overlap with them.

Reflows, on the other hand, occur when changes are made to an element’s layout properties, such as its width, height, or position on the page. These changes can also be triggered by a variety of events, such as adding or removing elements from the DOM or changing the font size of a parent element. Reflows are also computationally expensive, because they require the browser to recalculate the positions of all elements on the page and adjust their layout accordingly.

Both repaints and reflows can have a negative impact on the performance of a webpage, especially if they are triggered frequently or on large pages. As a result, it is important for web developers to minimize the number of repaints and reflows that occur on a page, and to optimize the performance of the page by minimizing the amount of work that the browser has to do during these processes.

Here are a few examples of ways to minimize reflows and repaints in a webpage:

  1. Avoid making unnecessary changes to the DOM: Every time an element is added, removed, or modified in the DOM, it can trigger a reflow. To minimize reflows, try to avoid making unnecessary changes to the DOM. For example, instead of adding and removing elements from the DOM repeatedly, consider hiding and showing them using CSS.
  2. Use CSS transitions and animations: Instead of using JavaScript to make visual changes to elements, consider using CSS transitions and animations. These changes can be performed smoothly and efficiently by the browser, without triggering a repaint.
  3. Use layout techniques that minimize reflows: There are certain techniques that can minimize the impact of reflows on page performance. For example, using “position: absolute” or “position: fixed” on elements can reduce the number of elements that need to be repositioned during a reflow, because these elements are removed from the normal flow of the document.
  4. Use requestAnimationFrame: The requestAnimationFrame() function is a high-performance function that allows you to schedule updates to the visual appearance of a webpage. By using requestAnimationFrame() instead of setTimeout() or setInterval(), you can ensure that visual updates are performed at the appropriate times, reducing the number of unnecessary repaints.
  5. Use hardware acceleration: Some visual changes, such as transforms and opacity changes, can be accelerated by the browser’s graphics hardware. By using the transform and opacity properties, you can take advantage of hardware acceleration and reduce the impact of repaints on page performance.

--

--

I’m a full-time software engineer who loves sharing knowledge to help others become better developers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ayesha Malik

I’m a full-time software engineer who loves sharing knowledge to help others become better developers.