JS: Concurrancy and Event Loop

博客介绍了JavaScript基于事件循环的并发模型,事件循环会同步等待消息,且每条消息处理完才处理下一条,JavaScript不会阻塞,I/O通过事件和回调处理。还提到Node JS架构采用单线程事件循环,其认为I/O成本高,每个连接对应线程耗内存大,所有I/O是事件驱动且异步的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Event Loop

JavaScript has a concurrency model based on an "event loop". The event loop got its name because of how it's usually implemented, which usually resembles:

while (queue.waitForMessage()) {
  queue.processNextMessage();
}

queue.waitForMessage() waits synchronously for a message to arrive if there is none currently.

  1. Each message is processed completely before any other message is processed.
  2. setTimeout message: a minimum time and not a guaranteed time.
  3. A web worker or a cross-origin iframe has its own stack, heap, and message queue. 
  4. A very interesting property of the event loop model is that JavaScript, unlike a lot of other languages, never blocks. Handling I/O is typically performed via events and callbacks

Node JS Architecture – Single Threaded Event Loop

  • The first basic thesis of node.js is that I/O is expensive.
  • The second basic thesis is that thread-per-connection is memory-expensive.
  • There is no way of making code run in parallel within a single request. However, all I/O is evented and asynchronous.
  •  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值