当前位置:网站首页>Browser thread
Browser thread
2022-07-06 08:28:00 【Wind billows】
1. Browser threads
1.JS Engine threads
JS kernel , Also known as JS engine , Be responsible for handling and executing javascript Script . This is a JS The main thread in , therefore JS It's a single threaded programming language , But actually JS It belongs to single thread asynchronous . Asynchrony is completed by sub threads , The main thread is responsible for scheduling sub threads . therefore JS Loosely speaking, it also belongs to multithreaded programming language
And GUI Threads are mutually exclusive
2.GUI Rendering thread
Responsible for rendering the browser interface , Including parsing HTML、CSS、 structure DOM Trees 、Render Trees 、 Layout and drawing
When the interface needs to be redrawn (Repaint) Or cause reflux due to an operation (reflow) when , The thread will execute
And JS Main threads are mutually exclusive
3. Event listener thread
Used to complete event processing , for example :click,mouseover…
4. Timer thread
Responsible for processing timers .setInterval and setTimeout
5. Network thread
Responsible for handling http The network requested
6. Thread priority
Which thread's bound handler is triggered first , Whoever will do it first
Send a request : What's going on Five layer network model : The physical layer 、 Data link layer 、 The network layer (IP)、 Transport layer (TCP、udp)、 application layer
Establishing a connection : Three handshakes, four waves
2. Rendering process of browser
The rendering process of each browser kernel is different , Now we mainly focus on webkit Mainly .
Render Prelude
- Browser input url, The browser main process takes over , Opened a download thread
- Then proceed HTTP request (DNS Inquire about 、IP Addressing, etc ), Waiting response , Start downloading response message .
- Transfer the downloaded content to Renderer Process management
- Start rendering …
Render pre concept
1.DOM Tree: The browser will HTML Data structure parsed into tree .
2.CSS Rule Tree: The browser will CSS Data structure parsed into tree .
3.Render Tree:DOM Trees and CSS Rule trees are merged to produce Render Trees .
4.layout: With Render Tree, The browser already knows which nodes are in the web page 、 Of each node CSS Definition and their affiliation , To calculate the position of each node in the screen .
5.painting: According to the rules , Through the video card , Draw the content on the screen .
6.reflow( backflow ): When the browser finds that there is a point change in a part that affects the layout , You need to go back and re render , Experts call this retreat process reflow.reflow From This root frame Start recursion down , Calculate the geometric dimensions and positions of all nodes in turn .reflow It's almost inevitable . Some popular effects on the interface now , For example, the folding of tree directory 、 an ( In essence, it is the explicit expression of elements Show and hide ) etc. , Will cause the browser's reflow. The mouse slip 、 Click on …… As long as these behaviors cause the footprint of some elements on the page 、 Positioning mode 、 Changes in properties such as margins , Will cause its internal 、 Re configuration of the surrounding and even the whole page dye . Usually we can't predict what the browser will reflow Which part of the code , They all affect each other .
7.repaint( Repaint ): Change the background color of an element 、 Text color 、 When the border color and so on do not affect the properties of its surrounding or internal layout , Part of the screen needs to be redrawn , But the geometry of the element doesn't change .
Be careful :display:none Nodes will not be added Render Tree, and visibility: hidden will , therefore display:none Will trigger reflow,visibility: hidden Will trigger repaint.
Rendering steps
After the browser kernel gets the response message , Rendering is roughly divided into the following steps
- analysis html production DOM Trees .
- analysis CSS The rules .
- according to DOM Tree and CSS Tree Generate Render Tree.
- according to Render The tree goes on layout, Responsible for the size of each element node 、 Position calculation .
- draw Render Trees (painting), Draw page pixel information .
- The browser will send the information of each layer to GPU,GPU Will synthesize layers (composite), It's on the screen .
JS Asynchronous loading scheme
because JS Will block subsequent code execution , So I hope the execution will not block the code . So officials and developers have designed many solutions .
1.defer
script Add... To the label defer attribute , Load asynchronously
1. But wait dom All documents have been parsed (dom Tree generated ) To be executed .
2. Only IE It works ;
2.async
script Add... To the label async attribute , Load asynchronously
1. Execute after loading ;async Only external scripts can be loaded
2. Can't take js Written in script In the label .
3.w3c standard ,IE9 The following is not supported
3. Load on demand
// Load on demand Encapsulates the code
function loadScript(url,callback){
// 1. establish script label
var script = document.createElement('script');
// 4. monitor script Whether the download is complete
// ie in onreadystatechange The state of the listening object changes
if(script.readyState){
// If there is readState This attribute To listen again script The status of the tag changes
script.onreadystatechange = function(){
// Judge script Whether loading is complete
if(script.readyState == 'loaded' || script.readyState == 'complate'){
callback();
}
}
}else{
script.onload = function(){
callback();
}
}
// 2.
script.src = url; // Load asynchronously js file It has changed. The download is complete , The state will not change again , So the monitoring event will not be triggered
// 3. Append labels to body in
document.body.appendChild(script);
}
loadScript('01.js',demo);
4. summary
In actual development, we only need to put script The tag is placed at the bottom of the page . There is absolutely no need to write redundant code .
5.js Timeline
step
1、 establish Document object , Start parsing web page . analysis HTML Elements and their text content are added after Element Objects and Text Node to document . This stage document.readyState = ‘loading’, Loading .
2、 encounter link external css, Create thread load , And continue to parse the document .
3、 encounter script external js, And no settings async、defer, Browser load , And block , wait for js Load complete and execute the script , Then continue to parse the document .
4、 encounter script external js, And it has async、defer, Browser create thread load , And continue to parse the document .
about async Script for properties , Execute the script as soon as it's loaded .( Asynchronous forbidden document.write())
5、 encounter img etc. , First, parse it normally dom structure , Then the browser loads asynchronously src, And continue to parse the document .
img 、input、video 、 audio Replaceable elements
div Irreplaceable element
6、 When the document is parsed ,document.readyState = ‘interactive’, Parsing completion status .
7、 After the document is parsed , All settings have defer The scripts will be executed in order .( Pay attention to and async Different , But it is also forbidden to use document.write());
8、document Object triggering DOMContentLoaded event , This also marks the program execution from the synchronization script execution phase , Turn into an event driven phase .
9、 When all async After the script is loaded and executed 、img Wait until the load is complete ,document.readyState = ‘complete’,window Object triggering load event .
10、 From now on , Handle user input in an asynchronous response 、 Internet events, etc .
summary
1.document Three states
loading Loading status ,dom The tree is being drawn
interactive Active state ,dom Tree drawing complete
complete Completion status ( Older browsers may be loaded),dom The tree drawing is completed and all resources are downloaded
2. Two events
DOMContentLoaded dom This event is triggered when the tree drawing is completed ( This is a js The only event in that contains capital letters , This event can only be bound using an event listener ).
load dom The event triggered after the tree drawing is completed and all resources are downloaded
边栏推荐
- 2022.02.13 - NC002. sort
- VMware virtualization cluster
- [2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
- 升级 TiDB Operator
- logback1.3. X configuration details and Practice
- Modify the video name from the name mapping relationship in the table
- 华为云OBS文件上传下载工具类
- Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
- All the ArrayList knowledge you want to know is here
- Analysis of Top1 accuracy and top5 accuracy examples
猜你喜欢
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
C语言 - 位段
matplotlib. Widgets are easy to use
Beijing invitation media
Synchronized solves problems caused by sharing
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
NFT smart contract release, blind box, public offering technology practice -- contract
Make learning pointer easier (3)
3. File operation 3-with
随机推荐
Erc20 token agreement
JVM 快速入门
Verrouillage [MySQL]
sys. argv
hcip--mpls
IoT -- 解读物联网四层架构
What is the use of entering the critical point? How to realize STM32 single chip microcomputer?
2022.02.13 - NC004. Print number of loops
Online yaml to CSV tool
Vocabulary notes for postgraduate entrance examination (3)
[secretly kill little partner pytorch20 days -day01- example of structured data modeling process]
JS select all and tab bar switching, simple comments
Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
Char to leading 0
LDAP application (4) Jenkins access
灰度升级 TiDB Operator
C language custom type: struct
Permutation and combination function
ESP series pin description diagram summary
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)