Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Node.js I/O blocking and I/O non blocking.

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 479
    Comment on it

    As we all know Node.js is a single threaded application in which we will heavily use event and callback. But before starting event loop we should know about events and cost of I/O & non-blocking I/O.So starting with event driven programming.

    Event Driven Programming:

    Event driven programming is a programming language in which the flow of the program is determined by events such a user actions(mouse click,key press). Event is very much used in GUI application where application listens an event and execute an action once the events occur.

    Source: https://en.wikipedia.org/wiki/Event-driven_programming

     

    I/O blocking & non-blocking I/O: In simple function I/O will wait for the returning of function when the first function will execute and return the output then other function will execute that is called as I/O blocking whereas in nodeJs when the first event is called there we pass a callback so it will not wait for returning any function and execute further. By this way  node.js save lots of time as compared to other frameworks. That process is called as nonblocking I/O.

     

    Example of Non-Blocking I/O and I/O Blocking:

    /* Within main.js write non blocking code */
    
    var firstFunction = function () {  
     console.log("I'm first!");
    };
    
    var secondFunction = function () {  
     setTimeout(firstFunction, 5000);
     console.log("I'm second!");
    };
    
    secondFunction();
    
    /* Results:
     * => I'm second!
     * (And 5 seconds later)
     * => I'm first!
     */
    
    
    /* Within main.js write blocking code */
    
    var firstFunction = function () {  
     console.log("I'm first!");
    };
    
    var secondFunction = function () {  
     firstFunction();
     console.log("I'm second!");
    };
    
    secondFunction();
    
    /* Results:
     * => I'm first!
     * => I'm second!
     */
    

     

    Here In the above example we can see the result I am second! will print first. It's mean that it will not wait for firstFunction result that type of function is called as Non Blocking I/O function which will helpful to make our site or application more powerful and consume less time to execute any function.

    By this way now we are able to understand the concept of I/O non blocking and I/O blocking.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: