René's URL Explorer Experiment


Title: JS浏览器事件循环机制 · Issue #2 · webproblem/Blog · GitHub

Open Graph Title: JS浏览器事件循环机制 · Issue #2 · webproblem/Blog

X Title: JS浏览器事件循环机制 · Issue #2 · webproblem/Blog

Description: 先来明白些概念性内容。 进程、线程 进程是系统分配的独立资源,是 CPU 资源分配的基本单位,进程是由一个或者多个线程组成的。 线程是进程的执行流,是CPU调度和分派的基本单位,同个进程之中的多个线程之间是共享该进程的资源的。 浏览器内核 浏览器是多进程的,浏览器每一个 tab 标签都代表一个独立的进程(也不一定,因为多个空白 tab 标签会合并成一个进程),浏览器内核(浏览器渲染进程)属于浏览器多进程中的一种。 浏览器内核有多种线程在工作。 GUI 渲染线程: 负责渲...

Open Graph Description: 先来明白些概念性内容。 进程、线程 进程是系统分配的独立资源,是 CPU 资源分配的基本单位,进程是由一个或者多个线程组成的。 线程是进程的执行流,是CPU调度和分派的基本单位,同个进程之中的多个线程之间是共享该进程的资源的。 浏览器内核 浏览器是多进程的,浏览器每一个 tab 标签都代表一个独立的进程(也不一定,因为多个空白 tab 标签会合并成一个进程),浏览器内核(浏览器渲染进程)属于...

X Description: 先来明白些概念性内容。 进程、线程 进程是系统分配的独立资源,是 CPU 资源分配的基本单位,进程是由一个或者多个线程组成的。 线程是进程的执行流,是CPU调度和分派的基本单位,同个进程之中的多个线程之间是共享该进程的资源的。 浏览器内核 浏览器是多进程的,浏览器每一个 tab 标签都代表一个独立的进程(也不一定,因为多个空白 tab 标签会合并成一个进程),浏览器内核(浏览器渲染进程)属于...

Opengraph URL: https://github.com/webproblem/Blog/issues/2

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"JS浏览器事件循环机制","articleBody":"先来明白些概念性内容。\r\n\r\n## 进程、线程\r\n\r\n* 进程是系统分配的独立资源,是 CPU 资源分配的基本单位,进程是由一个或者多个线程组成的。\r\n\r\n* 线程是进程的执行流,是CPU调度和分派的基本单位,同个进程之中的多个线程之间是共享该进程的资源的。\r\n\r\n## 浏览器内核\r\n\r\n* 浏览器是多进程的,浏览器每一个 tab 标签都代表一个独立的进程(也不一定,因为多个空白 tab 标签会合并成一个进程),浏览器内核(浏览器渲染进程)属于浏览器多进程中的一种。\r\n\r\n* 浏览器内核有多种线程在工作。\r\n\r\n    * GUI 渲染线程: \r\n        * 负责渲染页面,解析 HTML,CSS 构成 DOM 树等,当页面重绘或者由于某种操作引起回流都会调起该线程。\r\n        * 和 JS 引擎线程是互斥的,当 JS 引擎线程在工作的时候,GUI 渲染线程会被挂起,GUI 更新被放入在 JS 任务队列中,等待 JS 引擎线程空闲的时候继续执行。\r\n    \r\n    * JS 引擎线程: \r\n        * 单线程工作,负责解析运行 JavaScript 脚本。\r\n        * 和 GUI 渲染线程互斥,JS 运行耗时过长就会导致页面阻塞。\r\n\r\n    * 事件触发线程: \r\n        * 当事件符合触发条件被触发时,该线程会把对应的事件回调函数添加到任务队列的队尾,等待 JS 引擎处理。\r\n    \r\n    * 定时器触发线程: \r\n        * 浏览器定时计数器并不是由 JS 引擎计数的,阻塞会导致计时不准确。\r\n        * 开启定时器触发线程来计时并触发计时,计时完成后会被添加到任务队列中,等待 JS 引擎处理。\r\n    \r\n    * http 请求线程: \r\n        * http 请求的时候会开启一条请求线程。\r\n        * 请求完成有结果了之后,将请求的回调函数添加到任务队列中,等待 JS 引擎处理。\r\n\r\n    \r\n          ![image](https://user-images.githubusercontent.com/20440496/42146675-c35e6bdc-7dfb-11e8-920b-2c626c9845f5.png)\r\n\r\n\r\n## JavaScript 引擎是单线程\r\n\r\nJavaScript 引擎是单线程,也就是说每次只能执行一项任务,其他任务都得按照顺序排队等待被执行,只有当前的任务执行完成之后才会往下执行下一个任务。\r\n\r\nHTML5 中提出了 Web-Worker API,主要是为了解决页面阻塞问题,但是并没有改变 JavaScript 是单线程的本质。了解 [Web-Worker](https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API)。\r\n\r\n## JavaScript 事件循环机制\r\n\r\nJavaScript 事件循环机制分为浏览器和 Node 事件循环机制,两者的实现技术不一样,浏览器 Event Loop 是 HTML 中定义的规范,Node Event Loop 是由 libuv 库实现。这里主要讲的是浏览器部分。\r\n\r\nJavascript 有一个 main thread 主线程和 call-stack 调用栈(执行栈),所有的任务都会被放到调用栈等待主线程执行。\r\n\r\n* JS 调用栈\r\n    \r\n    JS 调用栈是一种后进先出的数据结构。当函数被调用时,会被添加到栈中的顶部,执行完成之后就从栈顶部移出该函数,直到栈内被清空。\r\n\r\n* 同步任务、异步任务\r\n\r\n    JavaScript 单线程中的任务分为同步任务和异步任务。同步任务会在调用栈中按照顺序排队等待主线程执行,异步任务则会在异步有了结果后将注册的回调函数添加到任务队列(消息队列)中等待主线程空闲的时候,也就是栈内被清空的时候,被读取到栈中等待主线程执行。任务队列是先进先出的数据结构。 \r\n\r\n* Event Loop\r\n\r\n    调用栈中的同步任务都执行完毕,栈内被清空了,就代表主线程空闲了,这个时候就会去任务队列中按照顺序读取一个任务放入到栈中执行。每次栈内被清空,都会去读取任务队列有没有任务,有就读取执行,一直循环读取-执行的操作,就形成了事件循环。\r\n\r\n    \r\n![image](https://user-images.githubusercontent.com/20440496/42146683-d0bd80ec-7dfb-11e8-9add-336129b63aca.png)\r\n \r\n![image](https://user-images.githubusercontent.com/20440496/42146690-d972b0d6-7dfb-11e8-8ca2-390c959abb9c.png)\r\n\r\n\r\n* 定时器    \r\n\r\n    定时器会开启一条定时器触发线程来触发计时,定时器会在等待了指定的时间后将事件放入到任务队列中等待读取到主线程执行。\r\n    \r\n    定时器指定的延时毫秒数其实并不准确,因为定时器只是在到了指定的时间时将事件放入到任务队列中,必须要等到同步的任务和现有的任务队列中的事件全部执行完成之后,才会去读取定时器的事件到主线程执行,中间可能会存在耗时比较久的任务,那么就不可能保证在指定的时间执行。\r\n\r\n* 宏任务(macro-task)、微任务(micro-task)\r\n\r\n    除了广义的同步任务和异步任务,JavaScript 单线程中的任务可以细分为宏任务和微任务。\r\n\r\n    macro-task包括:script(整体代码), setTimeout, setInterval, setImmediate, I/O, UI rendering。    \r\n\r\n    micro-task包括:process.nextTick, Promises, Object.observe, MutationObserver。\r\n\r\n    ```javascript\r\n        console.log(1);\r\n        setTimeout(function() {\r\n            console.log(2);\r\n        })\r\n        var promise = new Promise(function(resolve, reject) {\r\n            console.log(3);\r\n            resolve();\r\n        })\r\n        promise.then(function() {\r\n            console.log(4);\r\n        })\r\n        console.log(5);\r\n    ```\r\n\r\n    示例中,setTimeout 和 Promise被称为任务源,来自不同的任务源注册的回调函数会被放入到不同的任务队列中。\r\n\r\n    有了宏任务和微任务的概念后,那 JS 的执行顺序是怎样的?是宏任务先还是微任务先?\r\n\r\n    第一次事件循环中,JavaScript 引擎会把整个 script 代码当成一个宏任务执行,执行完成之后,再检测本次循环中是否寻在微任务,存在的话就依次从微任务的任务队列中读取执行完所有的微任务,再读取宏任务的任务队列中的任务执行,再执行所有的微任务,如此循环。JS 的执行顺序就是每次事件循环中的宏任务-微任务。\r\n\r\n    * 上面的示例中,第一次事件循环,整段代码作为宏任务进入主线程执行。\r\n    * 遇到了 setTimeout ,就会等到过了指定的时间后将回调函数放入到宏任务的任务队列中。\r\n    * 遇到 Promise,将 then 函数放入到微任务的任务队列中。\r\n    * 整个事件循环完成之后,会去检测微任务的任务队列中是否存在任务,存在就执行。\r\n    * 第一次的循环结果打印为: 1,3,5,4。\r\n    * 接着再到宏任务的任务队列中按顺序取出一个宏任务到栈中让主线程执行,那么在这次循环中的宏任务就是 setTimeout 注册的回调函数,执行完这个回调函数,发现在这次循环中并不存在微任务,就准备进行下一次事件循环。\r\n    * 检测到宏任务队列中已经没有了要执行的任务,那么就结束事件循环。\r\n    * 最终的结果就是 1,3,5,4,2。\r\n\r\n## 参考\r\n\r\n* [https://juejin.im/post/59e85eebf265da430d571f89](https://juejin.im/post/59e85eebf265da430d571f89)\r\n\r\n* [https://juejin.im/post/59c25c936fb9a00a3f24e114](https://juejin.im/post/59c25c936fb9a00a3f24e114)\r\n\r\n* [https://segmentfault.com/a/1190000012925872](https://segmentfault.com/a/1190000012925872)\r\n\r\n* [https://zhuanlan.zhihu.com/p/26229293](https://zhuanlan.zhihu.com/p/26229293)    ","author":{"url":"https://github.com/webproblem","@type":"Person","name":"webproblem"},"datePublished":"2018-07-02T05:29:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2/Blog/issues/2"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:951b32f7-c821-7244-0996-696ace5c1b08
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB026:12A68A:26AD3F:31F2BF:697679B9
html-safe-noncebcddc58117c41e78bd71aec7a22c10abe84386da15d8d95e6a7760b606be2466
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDI2OjEyQTY4QToyNkFEM0Y6MzFGMkJGOjY5NzY3OUI5IiwidmlzaXRvcl9pZCI6IjE0MzcwMzQ5MjAxNTE1MTM1MjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac41e1b2e7d253afe4d259a6f9eb47282eaac494147aeef2fb4b7f5cdcc049cc40
hovercard-subject-tagissue:337374628
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/webproblem/Blog/2/issue_layout
twitter:imagehttps://opengraph.githubassets.com/74c6a25c3794c5d6a2e8094e8b301cb96f74bde576e62a1a22e9bf2a7eff3c55/webproblem/Blog/issues/2
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/74c6a25c3794c5d6a2e8094e8b301cb96f74bde576e62a1a22e9bf2a7eff3c55/webproblem/Blog/issues/2
og:image:alt先来明白些概念性内容。 进程、线程 进程是系统分配的独立资源,是 CPU 资源分配的基本单位,进程是由一个或者多个线程组成的。 线程是进程的执行流,是CPU调度和分派的基本单位,同个进程之中的多个线程之间是共享该进程的资源的。 浏览器内核 浏览器是多进程的,浏览器每一个 tab 标签都代表一个独立的进程(也不一定,因为多个空白 tab 标签会合并成一个进程),浏览器内核(浏览器渲染进程)属于...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamewebproblem
hostnamegithub.com
expected-hostnamegithub.com
None032152924a283b83384255d9489e7b93b54ba01da8d380b05ecd3953b3212411
turbo-cache-controlno-preview
go-importgithub.com/webproblem/Blog git https://github.com/webproblem/Blog.git
octolytics-dimension-user_id20440496
octolytics-dimension-user_loginwebproblem
octolytics-dimension-repository_id115346710
octolytics-dimension-repository_nwowebproblem/Blog
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id115346710
octolytics-dimension-repository_network_root_nwowebproblem/Blog
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5b577f6be6482e336e3c30e8daefa30144947b17
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/webproblem/Blog/issues/2#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fwebproblem%2FBlog%2Fissues%2F2
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fwebproblem%2FBlog%2Fissues%2F2
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=webproblem%2FBlog
Reloadhttps://patch-diff.githubusercontent.com/webproblem/Blog/issues/2
Reloadhttps://patch-diff.githubusercontent.com/webproblem/Blog/issues/2
Reloadhttps://patch-diff.githubusercontent.com/webproblem/Blog/issues/2
webproblem https://patch-diff.githubusercontent.com/webproblem
Bloghttps://patch-diff.githubusercontent.com/webproblem/Blog
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fwebproblem%2FBlog
Fork 15 https://patch-diff.githubusercontent.com/login?return_to=%2Fwebproblem%2FBlog
Star 113 https://patch-diff.githubusercontent.com/login?return_to=%2Fwebproblem%2FBlog
Code https://patch-diff.githubusercontent.com/webproblem/Blog
Issues 10 https://patch-diff.githubusercontent.com/webproblem/Blog/issues
Pull requests 0 https://patch-diff.githubusercontent.com/webproblem/Blog/pulls
Actions https://patch-diff.githubusercontent.com/webproblem/Blog/actions
Projects 0 https://patch-diff.githubusercontent.com/webproblem/Blog/projects
Security 0 https://patch-diff.githubusercontent.com/webproblem/Blog/security
Insights https://patch-diff.githubusercontent.com/webproblem/Blog/pulse
Code https://patch-diff.githubusercontent.com/webproblem/Blog
Issues https://patch-diff.githubusercontent.com/webproblem/Blog/issues
Pull requests https://patch-diff.githubusercontent.com/webproblem/Blog/pulls
Actions https://patch-diff.githubusercontent.com/webproblem/Blog/actions
Projects https://patch-diff.githubusercontent.com/webproblem/Blog/projects
Security https://patch-diff.githubusercontent.com/webproblem/Blog/security
Insights https://patch-diff.githubusercontent.com/webproblem/Blog/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/webproblem/Blog/issues/2
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/webproblem/Blog/issues/2
JS浏览器事件循环机制https://patch-diff.githubusercontent.com/webproblem/Blog/issues/2#top
JavaScripthttps://github.com/webproblem/Blog/issues?q=state%3Aopen%20label%3A%22JavaScript%22
https://github.com/webproblem
https://github.com/webproblem
webproblemhttps://github.com/webproblem
on Jul 2, 2018https://github.com/webproblem/Blog/issues/2#issue-337374628
https://user-images.githubusercontent.com/20440496/42146675-c35e6bdc-7dfb-11e8-920b-2c626c9845f5.png
Web-Workerhttps://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API
https://user-images.githubusercontent.com/20440496/42146683-d0bd80ec-7dfb-11e8-9add-336129b63aca.png
https://user-images.githubusercontent.com/20440496/42146690-d972b0d6-7dfb-11e8-8ca2-390c959abb9c.png
https://juejin.im/post/59e85eebf265da430d571f89https://juejin.im/post/59e85eebf265da430d571f89
https://juejin.im/post/59c25c936fb9a00a3f24e114https://juejin.im/post/59c25c936fb9a00a3f24e114
https://segmentfault.com/a/1190000012925872https://segmentfault.com/a/1190000012925872
https://zhuanlan.zhihu.com/p/26229293https://zhuanlan.zhihu.com/p/26229293
JavaScripthttps://github.com/webproblem/Blog/issues?q=state%3Aopen%20label%3A%22JavaScript%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.