Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there significant differences between the Chrome browser event loop versus the node event loop?

Philip Roberts does a brilliant job explaining the browser event loop here providing a clear explanation between the call stack, event loop, task queue, and then "outside" threads like webapis. My question is do these parallel the equivalent components in the Node event loop and are they called basically the same thing. That is, when I make a call using Node's file and web i/o libraries, these are things that happen outside the stack whose callbacks are queued in a task queue?

like image 357
JohnGalt Avatar asked Sep 09 '14 17:09

JohnGalt


People also ask

Is event loop of JavaScript and node js same?

The event loop model is exactly the same. The only difference was node uses a thread pool to manage disk I/O - but it's now similar again with browser's implementation of threads of webworkers. The structure of network I/O and the event loop that is based around it is the same.

Does node have an event loop?

The event loop is what allows Node. js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background.

Do browsers have an event loop?

With a queueing mechanism in place, high priority tasks can be executed in a timely manner. This is the browser Event Loop. The common task is called MacroTask and the high priority task is called MicroTask. MacroTask includes: setTimeout , setInterval , requestAnimationFrame , Ajax, fetch, script tag code.

What is browser event loop?

JavaScript has a runtime model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks. This model is quite different from models in other languages like C and Java.

What is the difference between process nextTick () and setImmediate ()?

1. process. nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. setImmediate() method is used to execute a function right after the current event loop finishes.

How does NodeJS event loop work?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.


2 Answers

...when I make a call using Node's file and web i/o libraries, these are things that happen outside the stack whose callbacks are queued in a task queue?

Yes, absolutely; they're asynchronous just like Ajax and setTimeout are asynchronous. They perform some operation outside of the call stack, and when they've finished that operation, they add an event to the queue to be processed by the event loop.

Node's API provides a kind of asynchronous no-op, setImmediate. For that function, the "some operation" I've mention above is "do nothing", after which an item is immediately added to the end of the event queue.

There is a more powerful process.nextTick which adds an event to the front of the event queue, effectively cutting in line and making all other queued events wait. If called recursively, this can cause prolonged delay for other events (until reaching maxTickDepth).

like image 154
apsillers Avatar answered Oct 05 '22 21:10

apsillers


Both are quite different. Browser's event loop doesn't depends on I/O Operations. But Node js event loop depends on I/O operations. Here the Node js event loop main goal is to separate out the main process and try to execute I/O operations and other timer API's asynchronously.

And another difference is we don't have a function setImmediate() in Browser. Difference between setTimeout() and setImmediate() is In setTimeout() call back function will execute after the given minimum threshold value in milliseconds. But in setImmediate() once any I/O Operation is done, if particular code is given inside setImmediate(), it will be executed first.

Because normally

setTimeout(() => {     //some process }, 0); 

and

setImmediate(() => {     //some process }); 

are same and we can't predict which will execute first. But in Node js perspective under nodejs event loop mechanism if both are present under the callback of any I/O Operation, setImmediate() will get executed first. so,

let fs = require('fs'); fs.readFile('/file/path', () => {    setTimeout(() => {       console.log('1');    }, 0);    setImmediate(() => {       console.log('2');    }); }); 

The output for above code will be,

2 1 
like image 43
Prince Devadoss Avatar answered Oct 05 '22 20:10

Prince Devadoss