📜 ⬆️ ⬇️

How JS works: web page rendering engines and tips for optimizing their performance

Today, in the eleventh installment of the JavaScript series, we’ll talk about browser subsystems responsible for rendering web pages. They play a key role in transforming the descriptions of documents made with HTML and CSS into what we see on the screen.

image

The author of the material said that the company SessionStack has to pay great attention to rendering. In this article, he will share tips on optimizing web pages, taking into account the peculiarities of their visualization.


Overview


When creating web applications, we do not write isolated JS code that deals exclusively with some kind of own “internal” affairs. This code is executed in the environment provided by the browser and interacts with it. Understanding the device of this environment, how it works, what parts it consists of, allows the developer to create better programs, gives him the opportunity to anticipate possible problems with the application that came out.
')
The figure below shows the main components of the browser. Let's talk about the role they play in the processing of web pages.


The main components of the browser


In this article we will focus on the rendering engine. It is this browser subsystem that parses and renders HTML and CSS. And these are the technologies with which the code of web applications written in JavaScript constantly interacts.

On various rendering engines


The main task of the rendering engine is to display the requested page in a browser window. The engine can display HTML documents, XML documents, images. When using additional plugins, the engine can visualize other types of materials, for example, PDF documents.

We know that there are different JS engines that use different browsers. The same is true for rendering engines. Here are some popular engines:


Webpage Rendering Process


The rendering engine receives the contents of the requested document from the browser’s network layer. The rendering process looks like the one below.


Webpage Rendering Process

Here are the main steps of this process:


Consider these and other steps performed when rendering web pages in more detail.

Creating a DOM tree


The first step in the rendering engine is to parse the HTML document and convert what it did to DOM nodes located in the DOM tree. In this case, the web page, which is presented in the form of HTML code, is converted into a structure similar to that shown in the figure below.


DOM tree

Each element of this tree containing nested elements is a parent for them. This is true for all levels of nesting.

Creating a CSSOM Tree


CSSOM (CSS Object Model) is a CSS object model. When the browser is creating the DOM tree of a page, it finds in the head section a link tag that refers to an external CSS file, say, theme.css . Expecting that he may need this resource to render the page, the browser fulfills the request to download this file. This file contains plain text, which is a description of the styles applied to the page elements.

As in the case of HTML, the engine needs to convert CSS into something the browser can work with - in CSSOM. The result is a CSSOM tree, shown in the following figure.


CSSOM tree

Do you know why CSSOM has a tree structure? When the final set of styles for the page element is generated, the browser starts with the most general rules applicable to this element represented by the DOM node (for example, if the node is a child of the body element, all styles specified for the body are applied to it) and then recursively specifies computed styles by applying more specific rules.

Let us examine the example presented in the previous figure. Any text contained within the span tag that is placed on the body element is displayed in red and has a font size of 16px . These styles are inherited from the body element. If the span element is a descendant of the p element, then its contents are not displayed in accordance with the more specific style applied to it.

Also note that the above tree is not a full CSSOM tree. This shows only the styles that we, in our CSS file, decided to override. Each browser has a standard style set, applied by default, also known as “user agent styles” (user agent styles). It is the results of applying these styles that can be seen on a page that has no CSS rules associated with it. Our styles simply override some of the standard browser styles.

Creating a render tree


The instructions on the appearance of elements presented in HTML, combined with information about their styling from the CSSOM tree, are used to form the rendering tree.

What it is? This is a tree of visual elements created in the order in which they will be displayed on the screen. This is a visual representation of the HTML code of the page, reflecting the influence of the CSS rules corresponding to this page. The purpose of this tree is to ensure that the elements are displayed in the correct order.

The node of the rendering tree is known in the WebKit engine as "renderer" or "render object" (we will call them "rendering objects").

Here is what the rendering tree will look like for the DOM and CSSOM trees shown above.


Rendering tree

Here is a general description of the browser actions that it performs when creating the render tree.


In order to better understand what is being said here, you can take a look at the source code of the RenderObject class from WebKit. Each rendering object is a rectangular area, usually corresponding to the CSS block of the node. Information about this block includes its geometrical characteristics, such as width, height and position.

Forming page layout


After the rendering object is created and added to the tree, it has not yet been assigned a position and size. Calculating these values ​​is called page layout generation.

HTML uses a streaming layout model. This means that most often the system can calculate the geometric parameters of the elements in one pass. It uses a coordinate system based on the root rendering object, it uses the coordinates left and top .

Layout generation is a recursive process. It starts at the root object that corresponds to the <html> element of the document. The process is performed recursively throughout the hierarchical structure of the rendering object, the dimensions and position are calculated for each element that needs it.

The position of the root rendering object is 0,0 . Its dimensions correspond to the size of the visible part of the browser window (this is called the viewport).

The process of forming a layout means assigning to each node the exact position in which it should appear on the page.

Render tree rendering


At this stage, the rendering tree is traversed and the paint() methods of rendering objects are called, which perform the output of a graphical representation of the objects on the screen.

Visualization, or rendering, can be global or incremental (the page layout is also formed).


In general, it is important to understand that visualization is a phased process. To improve the perception of the page by users, the rendering engine tends to display the page as soon as possible. It will not wait until all HTML has been parsed, in order to proceed to the formation of the rendering tree and the calculation of the page layout parameters. As a result, some parts of the page will be processed and displayed, while the rendering engine will continue to work with the remaining content of the page, which comes from the network.

The order of processing JS-scripts and CSS-files


The analysis and execution of the script is carried out immediately after the page code processing system reaches the <script> . Document processing is suspended until the script is executed. This means that this process is performed synchronously.

If the script is received from an external source, it must first be downloaded via the network (also synchronously). Page processing pauses until the script download is complete.

HTML5 allows you to indicate the possibility of asynchronous loading and processing of the script using a separate stream.

Rendering performance optimization


If you want to optimize your application, taking into account the features of page rendering, there are five main areas that you can control and which need to be addressed.


JS code optimization


JavaScript code often leads to changes in what can be observed in the browser. This is especially true for single-page applications. Here are some tips on optimizing JS to improve the page rendering process.


CSS optimization


Modifying the DOM by adding and removing elements, changing attributes, and other similar actions will cause the browser to recalculate the styles of the elements, and, in many cases, the layout of the entire page, or at least some part of it. To optimize the page rendering process, consider the following.


Layout optimization


Recalculation of the page layout may require serious system resources. To optimize this process, consider the following.


Rendering optimization


Often this task takes the most time, so it is important to avoid situations that lead to redrawing the page. Here is what can be done here.


Results


In this article we talked about the rendering subsystems of modern browsers. The right approach to page visualization leads to improved performance of web applications and improved user experience.

Previous parts of a series of articles:

Part 1: How JS Works: Overview of the Engine, Runtime Mechanisms, Call Stack
Part 2: How JS Works: About V8 Inside and Code Optimization
Part 3: How JS works: memory management, four types of memory leaks and how to deal with them
Part 4: How JS works: event loop, asynchrony, and five ways to improve code with async / await
Part 5: How JS: WebSocket and HTTP / 2 + SSE work. What to choose?
Part 6: How JS Works: Features and Scope of WebAssembly
Part 7: How JS Works: Web Workers and Five Use Cases
Part 8: How JS Works: Service Workers
Part 9: How JS Works: Web Push Notifications
Part 10: How JS Works: Tracking DOM Changes with MutationObserver

Dear readers! What techniques do you use to optimize the rendering of the pages of your web projects?

Source: https://habr.com/ru/post/351802/


All Articles