当前位置:网站首页>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
边栏推荐
- From monomer structure to microservice architecture, introduction to microservices
- 1202 character lookup
- 从 CSV 文件迁移数据到 TiDB
- Research and investment forecast report of citronellol industry in China (2022 Edition)
- 从表中名称映射关系修改视频名称
- vulnhub hackme: 1
- Ruffian Heng embedded bimonthly, issue 49
- Analysis of pointer and array written test questions
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- IOT -- interpreting the four tier architecture of the Internet of things
猜你喜欢

Pyqt5 development tips - obtain Manhattan distance between coordinates

【刷题】牛客网面试必刷TOP101

Yyds dry goods inventory three JS source code interpretation eventdispatcher

Résumé des diagrammes de description des broches de la série ESP

JVM performance tuning and practical basic theory - Part 1

2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers

MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object

2022.02.13 - NC004. Print number of loops

IOT -- interpreting the four tier architecture of the Internet of things

704 二分查找
随机推荐
TiDB备份与恢复简介
使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
ROS编译 调用第三方动态库(xxx.so)
IOT -- interpreting the four tier architecture of the Internet of things
[MySQL] database stored procedure and storage function clearance tutorial (full version)
Migrate data from a tidb cluster to another tidb cluster
MySQL learning record 07 index (simple understanding)
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
Remote storage access authorization
vulnhub hackme: 1
Fibonacci sequence
[research materials] 2022 China yuancosmos white paper - Download attached
Leetcode question brushing (5.28) hash table
Make learning pointer easier (3)
Use Alibaba icon in uniapp
[brush questions] top101 must be brushed in the interview of niuke.com
JVM performance tuning and practical basic theory - Part 1
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
synchronized 解决共享带来的问题