Process management in Event loop
setTimeout() :-
Definition: setTimeout is used to schedule a function after some time in future ,i.e the function will run after given time.
Example:
console.log("first") ;
setTimeout(function(){
console.log("second");
},2000);
console.log('third');
setTimeout(function(){
console.log("second");
},2000);
console.log('third');
Output:
first
third
second
To understand setImmediate we must first understand few terms:
Event Queue: This is a queue where all the callbacks/events gets stored while event loop is busy.
Tick: Each time the event loop takes callbacks/events from the event Queue for processing this is called tick.
setImmediate() :-
Defination: setImmediate is used to schedule a function at end of the event queue (after all callbacks/events present at that time in event queue).
This is used for heavy CPU processing operations. These heavy operations can be scheduled at last using above functions so that other callbacks/events does not wait in the queue for completion of heavy tasks.
Example:
console.log("first");
setImmediate(function(){
console.log("second");
});
console.log("third");
setImmediate(function(){
console.log("second");
});
console.log("third");
Output:
first
third
second
Note: SetImmediate takes precedence over setTimeout & setImterval but not other callbacks.
process.nextTick(): -
Defination: process.nextTick is used to schedule a function at front of event queue(before all callbacks/ event s waiting in the event queue).
This method is used when you want to run some function in a fresh call stack like highly recursive functions.Since process.nextTick() puts the function on top of the event queue.So after completion of the current executing code , the event loop starts processing next function present in event queue which is you process.nextTick function in an fresh call stack.
Example:
console.log("first");
process.nextTick(function(){
console.log("second");
});
console.log("third");
process.nextTick(function(){
console.log("second");
});
console.log("third");
Output:
first
third
second
Difference between setImmediate and process.nextTick:
process.nextTick adds in front of all callbacks and event loop while setImmediate adds at the end.
Example:
for(var i=0;i<3;i++){
fs.readFile("abc.mkv",function(err, data){
if(err){
console.log(err);
}else{
setImmediate(function(){
console.log("completed set immediate");
});
console.log("completed calback");
}
});
}
console.log('event emitted');
Output:
In the above example, I am reading a file in a loop for 3 times.After the file is read i.e the callback returns , i have used setImmediate with a function that needs to be added in the event queue.This is simple function that prints some message in the console.
As you can see here that, "completed set immediate" is printed at last . This shows that these were added at end of callback of read file.
Running the same function process.nextTick();
for(var i=0;i<3;i++){
fs.readFile("abc.mkv",function(err, data){
if(err){
console.log(err);
}else{
process.nextTick(function(){
console.log("completed set immediate");
});
console.log("completed calback");
}
});
}
Output:
As you can see in the output, completed set immediate is printed as soon the current callback execution is over.that means the process.nextTick adds in top of the event queue.
No comments:
Post a Comment