Field Guide to Advanced Python Types for Typescript Developers

Many developers developing polyglot systems or services will be working with the usual language suspects: Javascript, Python, Go, Java to name a few. Typescript’s unprecedented rise in popularity…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




DOM and The Web Page

A concise introduction of how the browser displays a web page.

Document Object Model (DOM) is an interface which treats the HTML (or XHTML/XML) as a tree structure. The node of this tree represents an HTML element.

The process of rendering a page on the web browser involves many steps. Simply speaking, when we type a URL in the address bar of the web browser and hit enter, the browser makes a call to the server and receives the response. The response only contains the texts and instructions.

The page that we see on the browser, the design, the colours, the dimensions of the containers, and so on is not something the browsers get from the server and display it.

At an abstract level, the browsers goes through the contents it gets and performs these steps to display the web pages.

Step 1: Construct the DOM: The browser parses the HTML content and creates the DOM, the tree structure with the nodes as mentioned above, sometimes also referred to as content-tree.

Step 2: Create the Render Tree: A render tree contains styling attributes as well as the dimensions of boxes. For example, a div with red as background, black border and 100 x 100 pixels in dimensions with some texts/images.

Step 3: Create the layout of the Render Tree: In this step, the browser constructs the view of the page, i.e. it determines which box will appear where on the screen based on given attributes.

Step 4: Painting the Render Tree: The browser traverses the render tree [with layout] and paints each node on the page.

Why do you see a frozen or flickering screen?

Now that we have established that the browser has to perform these tasks to render a web page, we can tell this is valid for every new web page that it has to display. But what is a new page?

When we click on a link, the page refreshes and, we see a different page. In case of an SPA (single page application), the browser does not go through a full page reload but displays a new content. In such scenarios, the browser gets a new page.

Now let us assume, we click on a close icon to remove an element from the page. Does it create a new page? We would say no. But for the browsers, it is a new page. Why?

Because when we remove an element from the page, it changes the structure of the page. Technically, the DOM tree changes, and if the DOM tree changes, we are back to step 1. If you have read the article I linked above, you know it’s an expensive job. Consider removing or even adding elements one by one, back to back.

Repaint Vs Reflow

A repaint happens when the browsers don’t have to go back to step 1 all the way. In this case, the structure of the page does not change. For example, consider modifying properties like opacity, visibility, background colour, font-colour, and so on.

A reflow happens when the structure of the page changes and the browsers have to go back to step 1. For example, hiding/showing/removing/adding elements on the page, changing dimension of a box, etc.

As a thumb rule, the number of reflows or repaints [DOM Manipulation] done on the page is directly proportional to the amount of work the browsers have to do. And if this happens, you get a frozen or flickering screen.

Add a comment

Related posts:

Precious First 60 Minutes During An Emergency

It was any other day for the young mother who dropped her kids off at school. But while she was crossing the road, a speeding truck crashed into her. The impact of the collision was so intense that…

The Pain Market

Think about the last time someone said ‘fuck your pain’ — or some variant of it — to you. Maybe someone said that your experiences didn’t matter. Maybe they said you had too much privilege to know…