问题
In JavaScript, the event loop is used in the engine. Here is one diagram to illustrate it from this article.
(source: mybalsamiq.com)
For Node.js, the event loop also implemented here. Quoting from this question.
The Node.js event loop runs under a single thread, this means the application code you write is evaluated on a single thread. Nodejs itself uses many threads underneath trough libuv, but you never have to deal with with those when writing nodejs code.
However, it is still abstract for me about node.js event loop.
- Is there any image to introduce it more clearly?
- What's the different between those two event loops?
回答1:
What's the difference between those two event loops?
Nothing. Nodejs is the JavaScript engine1.
1: Or rather, one of them, there are other engines implementing the same language and the same event loop concept.
Is there any image to introduce it more clearly?
There are many. But I think an animation is better :-) This jsconf talk by Philip Roberts is praised everywhere.
回答2:
The Nodejs event loop is implemented differently than the browser based event loop.
This is a point of huge confusion in the Nodejs community.
While Nodejs uses the Google V8 as it's runtime, it does not used V8 to implement the event loop.
Nodejs uses the Libuv library (written in C++) to implement the event loop.
The digram you have above, which works for the JS event loop, is not the same for the Nodejs event loop.
There are three references you should study in order to fully understand the Nodejs event loop:
- https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
- http://docs.libuv.org/en/v1.x/design.html
- https://www.youtube.com/watch?v=sGTRmPiXD4Y
来源:https://stackoverflow.com/questions/31582672/what-is-the-different-between-javascript-event-loop-and-node-js-event-loop