Mastering Browser Rendering: Repaint, Reflow, and Performance Optimization

This article explains the browser rendering pipeline, the differences between repaint and reflow, and provides practical tips and code examples to minimize costly layout recalculations for smoother web page performance.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Mastering Browser Rendering: Repaint, Reflow, and Performance Optimization

Rendering Process

(1) Convert HTML code into a Document Object Model (DOM).

(2) Load and parse CSS files to create a CSS Object Model.

(3) Using the DOM and CSS Object Model, organize visible elements into a render tree, where each render object combines a DOM node with its computed styles, effectively defining each object's appearance.

(4) Compute coordinates for each object in the render tree, defining their positions.

(5) Finally, the elements of the render tree are painted onto the screen.

Repaint

When a style change does not affect an element’s position—such as background-color, border-color, or visibility—the browser simply repaints the element with the new style.

Reflow

The browser performs a reflow when any of the following actions occur:

Adding, removing, or modifying DOM elements, or changing their order.

Content changes, including text edits within form fields.

Changing CSS properties.

Adding or removing style sheets.

Modifying class attributes.

Window operations like resizing or scrolling.

Activating pseudo‑classes (e.g., hover).

Repaint & Reflow Optimization

In practice, DOM manipulations are unavoidable, but to reduce performance costs, follow the principle of minimizing repaint and reflow triggers.

Example of optimized code (combined changes are applied after the script finishes, resulting in a single repaint/reflow):

var $body = $('body');
$body.css('padding', '1px'); // triggers repaint & reflow
$body.css('color', 'red');   // triggers repaint
$body.css('margin', '2px');  // triggers repaint & reflow

Example of inefficient code (reading a property forces the browser to execute pending repaint/reflow before continuing):

var $body = $('body');
$body.css('padding', '1px');
$body.css('padding'); // forces immediate repaint/reflow
$body.css('color', 'red');
$body.css('margin', '2px');
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

frontendperformanceRenderingBrowserreflowrepaint
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.