Banuprakash C
Full Stack Architect,
Co-founder Lucida Technologies Pvt Ltd.,
Corporate Trainer,
Email: banuprakashc@yahoo.co.in
https://www.linkedin.com/in/banu-prakash-50416019/
https://github.com/BanuPrakash/NODE
Softwares Required:
- Visual Studio Code. [https://code.visualstudio.com/download]
- Chrome Web Browser
- NodeJS Latest LTS Version: 16.x.x [https://nodejs.org/en/download/]
================================
JS ==> JS engine [ V8 { Google ==> Chrome, NodeJS}, SpiderMonkey [ FireFox, Adobe], Chkra, Nashorn, ...]
NodeJS
- Platform built on V8 engine runtime
- event-driven [Thread ], non-blocking I/O model [NIO]
libuv is a multi-platform C library that provides support for asynchronous I/O based on event loops. It is primarily designed for use in Node.js but it is also used by other software projects.
https://github.com/nodejs/node
event loop psuedocode
//myFile.js
const microTask = []; // PromiseAPI, async await, nextTick()
const pendingTimers = []; // macrotask ==> setTimeout, setInterval, setImmediate const pendingOsTasks = []; // macrotask const pendingOperations = []; // macrotask
function shouldContinue() { return pendingTimers.length || pendingOsTasks || pendingOperations.length .. }
// Running Thread while(shouldContinue()) { // is stack empty // 1 check all microTasks and empty it ==> push them to Stack // 2 check and empty all pendingTimers ==> push them to Stack // 3 pending OS tasks ==> Socket related, I/O callbacks ==> read chunk of data from file ==> push them to Stack // 4 Pending Operations ==> close of connections , etc ==> push them to Stack // pause execution }
Tick ==> one iteration of while()
======================
myfile.js
console.log("Hello!!!");
setTimeout(function one() { console.log("t1"); }, 0);
setTimeout(function two() { console.log("t2"); }, 0);
Promise.resolve().then(function p1() { console.log("p1"); });
Promise.resolve().then(function p2() { console.log("p2"); });
console.log("Bye!!!");
=====================
=====================================
function logA() {..} function logB() {..} function logC() {..} function logD() {..} function logE() {..} function logF() {..} function logG() {..}
logA();
setTimeout(logG, 0);
Promise.resolve() .then(logB) .then(setTimeout(logC)) .then(logD) .then(logE);
setTimeout(logF);
==================================
process.nextTick()
Every time the event loop takes a full trip we call it as tick.
// microtask process.nextTick(function task() { // do something });
==> function executes at the end of current tick.
setTimeout(function timed() {
}, 0);
===========================================
NodeJS V8 Memory Structure
-
ResidentSet
-
Code Segment ==> JIT compiler compiles code blocks
-
Heap
- New Generation [ SemiSpace 1, Semispace] ==> In Java ==> Surviour state 1 and Surviour state 2
- Old Generation
-
Stack
-
Large Objects [Code, MMap]
C#, JS and Java or any technology which uses Virtual machine doesn't allow pointers
node --min_semi_space_size(intial) --max_old_space_size(max) --stack-size(size) server.js
===================================================================
nodeJs provides built-in modules crypto, fs, http, path, repl, ...
Module System:
- IIFE ==> Immediately invoke function expression
let ShoppingModule = (function() { let data = 100; function doTask() {
}
return {
doTask
}
})();
let paymentModule = (function() { let data = 999; let done = false; function doTask() {
}
return {
done,
data,
doTask
}
})();
ShoppingModule.data ==> 100 // not visible paymentModule.data ==> 999
-
CommonJS module system ==> default module system in NodeJS
compute.js
module.exports.add = function(){ }
function sub() {
}
other.js
let compute = require('./compute');
can access compute.add() but not compute.sub()
-
ESM ==> ES 6 module system
export const add = function() {}
other.js
import {add} from './compute';
- AMD
- System
- UMD ==> wrapper for different module system to dynamically decide which module system has to be used
-
NodeJS Threads concept
-
NodeJS Async Operations Network related operations doesn't use libuv Threads
==================================================
node.js Event Emitter
=================
- fs sync
- fs callback
- fs stream based
Where can i use NodeJS?
- Real time application
- Streaming API ==> Netflix Squid Game chunk of data has to pushed to client; client buffers
- Traditional Web application ==> server rendered pages
- build RESTful Web services
- GraphQL services
==============
npm i -g learnyounode
learnyounode
=======================