How to calculate browser F12 Finish DOMContentLoaded load time?

how to calculate browser F12 Finish DOMContentLoaded load time?

Mar.01,2021

there are


first of all, you need to know the principle of browser rendering. DOMContentLoaded means that when the dom is loaded, you open a page that is blank at the beginning, and then the content will be displayed in an instant. From the blank page to the display content will trigger DOMContentLoaded
. Then the problem arises again, what is the HTML document is loaded and parsed. To solve this problem, we must understand the principle of browser rendering.

when we enter URL in the browser address, the browser will send a request to the server, and the server will send the requested HTML document back to the browser. After the browser downloads the document, it will begin to parse from top to bottom. After the parsing is completed, DOM will be generated. If there is a css, on the page, the CSSOM, will be formed according to the contents of the css, and then DOM and CSSOM will generate a render tree, and finally the browser will calculate the exact size and location of each node in the page based on the contents of the render tree and draw it on the browser. The parsing of
html will be interrupted by js again. When the < script > tag is encountered in the parsing process, the parsing process will be stopped and the script will be processed instead. If the script is inline, the browser will first execute the inline script. If it is outside the chain, the script will be loaded first and then executed. After processing the script, the browser continues to parse the HTML document.
so usually put the js file at the end
it is important to note that in current browsers, in order to slow down the blocking of rendering, modern browsers use guessing preloading. When parsing is blocked, the browser has a lightweight HTML (or CSS) scanner (scanner) that continues to scan the document, looking for url, of resource files that may be available in the future and downloading them before the renderer uses them.

here we can make clear the time calculated by DOMContentLoaded. When there is no script in the document, the browser can trigger the DOMContentLoaded event after parsing the document; if the document contains a script, the script will block the parsing of the document, and the script needs to wait for the css in front of the script to load before it can be executed. In any case, the trigger of DOMContentLoaded does not need to wait for other resources such as pictures to load.

reference: click here


timing starts from the first request on the page, to the end of all requests on the page, and to the completion of all the elements on the page. The time of the period is time

the browser monitors whether the request and drawing are completed


http://www.cnblogs.com/longm/.
calculation white screen time

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript">
    // performance.timing IE8
    window.pageStartTime = Date.now();
  </script>
  <!--  CSS  -->
  <link rel="stylesheet" href="common.css">
  <link rel="stylesheet" href="page.css">
  <script type="text/javascript">
    // 
    window.firstPaint = Date.now();
  </script>
</head>
<body>
  <!--  -->
</body>
</html>

so the white screen time can be calculated as follows:

when Performance API is available

White screen time = firstPaint-performance.timing.navigationStart;

time of the first screen
usually the method of calculating the first screen is

first screen module label marking method
Statistics the time when the slowest picture is loaded in the first screen
Custom first screen content calculation method

1. First screen module label method

the first screen module tag method is usually suitable for situations where the content of the first screen does not need to pull data to survive and the page does not consider the loading of resources such as images. We will record the current timestamp in the HTML document using inline JavaScript code corresponding to the end of the tag on the first screen. As follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript">
    window.pageStartTime = Date.now();
  </script>
  <link rel="stylesheet" href="common.css">
  <link rel="stylesheet" href="page.css">
</head>
<body>
  <!-- 1 -->
  <div class="module-1"></div>
  <!-- 2 -->
  <div class="module-2"></div>
  <script type="text/javascript">
    window.firstScreen = Date.now();
  </script>
  <!-- 3 -->
  <div class="module-3"></div>
    <!-- 4 -->
  <div class="module-4"></div>
</body>
</html>

when the first screen time is equal to firstScreen-performance.timing.navigationStart;

as a matter of fact, the tag marking method of the first screen module is relatively rare in the business, and most pages need to pull data through the API before they can be fully displayed, so we will use JavaScript script to determine the loading status of the first screen page content.

2. Count the time it takes for images to be loaded on the first screen

usually the slowest thing to load on the first screen is the image resource, so we will take the time when the slowest image is loaded on the first screen as the time for the first screen.

because browsers limit the number of TCP connections per page, not all images can be downloaded and displayed immediately. Therefore, after the construction of the DOM tree, we will traverse all the image tags in the first screen, listen for all image tag onload events, and finally traverse the maximum load time of the image tags, and subtract the navigationStart from this maximum value to get an approximate first screen time.

at this time, the first screen time is equal to the point in time when the slowest picture is loaded-performance.timing.navigationStart;

3. Custom module content calculation

it is complicated to calculate the time it takes for images to be loaded on the first screen. Therefore, in our business, we usually simplify the calculation of the first screen time by customizing the content of the module. As follows:

ignore the loading of resources such as pictures, and only consider the main DOM of the page.
only consider the main modules of the first screen, not all the content above the first screen line in a strict sense

.
Menu