GithubHelp home page GithubHelp logo

sahaj19 / nodejs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 522 KB

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript on the server side, creating server-side applications with JavaScript.

JavaScript 71.04% CSS 8.78% EJS 20.18%
cookies file-handling http-methods jwt middlewares modules multer mvc-pattern nodejs-streams server-side-rendering versioning websockets

nodejs's Introduction

Node.js Architecture

  • Initially, all requests coming from the client will be lined up in the event queue.
  • Then, the event loop picks up these requests from the event queue following the FIFO principle, and its job is to remove these requests from the event queue.
  • When the event loop picks up a request from the event queue, it can be of two types: non-blocking (async) or blocking (sync) requests.
  • If the request is non-blocking, the event loop processes it and returns the result to the user.
  • If the request is blocking, it is sent to a thread pool.
  • The thread pool consists of threads responsible for fulfilling these blocking requests.
  • Threads (workers) are assigned to handle these blocking requests if they are available in the thread pool.
  • After the worker completes its task, it returns to the thread pool and returns the result.

Important Points

  • By default, Node.js allocates only 4 threads in the thread pool.
  • If more than 4 users have written blocking code, the remaining users will experience increased waiting times, potentially leading to scalability issues.
  • A good practice is to follow non-blocking coding practices to avoid these issues.
  • A common strategy for maximizing performance is to set the maximum number of threads in the thread pool equal to the number of CPU cores available.

Example Setup

const fs = require("fs");

// Comment this line after executing it
fs.writeFile("./example.txt", "hello from nodejs", (error) => { 
    console.log(error);
});

Blocking

console.log("1");

// blocking...
let content = fs.readFileSync("./example.txt", "utf-8");
console.log(content);

console.log("2");

output

  • 1
  • hello from nodejs
  • 2

Non-blocking

console.log("1");
  
// non-blocking..
fs.readFile("./example.txt", "utf-8", (error, result) => {
    if(error) {
        console.log(error);
    } else {
        console.log(result);
    }
});

console.log("2");

output

  • 1
  • 2
  • hello from nodejs

nodejs's People

Contributors

sahaj19 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.