You can't optimize things that you can't measure. But the Navegation Timing API allows us to measure the critical rendering process time!
- Navigation Timing provides a high-precision timestamp for the calculation of CRP
- The browser sends a series of measurable times to capture the various states of CRP
The Navigation Timing API provides a good measurement:
Each label in the table above corresponds to a high-precision timestamp that the browser monitors when each page is loaded. We remove everything and network-related timestamps, showing only a few:
domLoading
: This is the timestamp at the beginning of the entire process, when the browser first receives the bytes of the HTML document when the parsing time begins.
domInteractive
: The timestamp when the browser completed all HTML parsing and the DOM construct was completed.
domContentLoaded
: When Dom is finished and no CSS style prevents JS from executing-it means that we can now construct the timestamp of the render tree--.
- Many JS frameworks wait for this event before they begin to execute their own logic-because the browser captures Eventstart and eventend timestamps to allow us to track how long this event executes.
domComplete
: As the name implies, all the resources on the page (such as samples, etc.) are downloaded and all the actions on the page are finished
loadEvent
: Additional application logic is triggered when the OnLoad event is started when the browser is loaded in the last step on each page
domInteractive
Mark Dom Complete
domContentLoaded
marking the completion of the DOM and Cssom
- If there is no JS blocking, then dominteractive will be domcontentloaded immediately.
domComplete
Mark when the page and all the resources are complete
<HTML> <Head> <title>Critical path:measure</title> <Metaname= "Viewport"content= "Width=device-width,initial-scale=1"> <Linkhref= "Style.css"rel= "stylesheet"> <Script> functionMEASURECRP () {varT=window.performance.timing, interactive=t.dominteractive-t.domloading, DCL=T.domcontentloadedeventstart-t.domloading, complete=T.domcomplete-t.domloading; varStats=Document.createelement ('P'); Stats.textcontent= 'Interactive:' +Interactive+ 'MS,' + 'DCL:' +DCL+ 'MS, complete:' + Complete+ 'Ms'; Document.body.appendChild (stats); } </Script> </Head> <Bodyonload= "MEASURECRP ()"> <P>Hello<span>Web Performance</span>students!</P> <Div><imgsrc= "Awesome-photo.jpg"></Div> </Body></HTML>
The code above is: the Navigation Timing API captures all relevant timestamps and our code waits for the OnLoad event to trigger-remember that the OnLoad event is in Dominteractive, Domcontentloaded and Domcomplete are triggered after--and each timestamp is calculated differently.
Just like what we say and do, I can use a function to simply trace all of these processes.
You can also embed the code of the analytics provider (Google Analytics does this automatically) in your code.
Original link: https://developers.google.com/web/fundamentals/performance/critical-rendering-path/measure-crp?hl=en
Calculate the time it takes to render a critical process