Introducing NodeJs

published on: | by cindy In category: Javascript

Alright, there is a lot of confusion on what node is and what it's not. We will dive into understanding node's architecture and the concepts behind it as well. Node is a runtime environment for executing JavaScript code outside the browser. It allows us to write server-side scripting code just like you would in Python, Php etc. it is best suited for the highly scalable, real-time and data-intensive application. A lot of questions has emerged if Node is a programming language or not, what kind of programs it is best suited for ! if Node is multi-threaded or not, how does it achieve its non-blocking abilities? etc.

Javascript Before Node

But before we say anything node, let's talk JavaScript. I will not dwell much on JavaScript, it's not our scope Node is. JavaScript, in a nutshell, is a client-side language that adds interactivity on web-pages. Runs on the browser and different browsers have different engines that execute JavaScript code to machine code.

Microsoft Edge uses Chakra, Mozilla uses Spidermonkey, chrome uses v8 engine. This difference in the hs engine is the reason why JavaScript behaves differently in different browsers. In conclusion, JavaScript is:

  1. Is a client-side scripting programming language
  2. it is single-threaded. It cannot run more than one operation at a time. At any given time, only one line of your code will be executing
  3. It is synchronous, you have to wait for one line of code to execute before you can execute the next.

Node history

Node came into existence in 2009 and was created by Ryan Daah. Dahl took the chromes v8 JavaScript engine (which is the fastest) and embedded it into c++ and called the program node. It has additional capabilities not available in the browser like reading and creating files. Node is asynchronous in nature. Wait, but Js is synchronous. You are damn right.

Node is characterized by: 1. Events-driven. Everything happening in Node is a reaction to an event 2. Non-blocking i/o.

Asynchronous JavaScript

At the heart of Node lies the event loop. That is not to say other js runtime environment does not use the event loop, Node gives you low-level access to the event loop and system resources. This is your best friend, the one that gives nodejs its asynchronous and non-blocking I/O behaviour. It is therefore important for us to understand the concept behind the event loop Before the event loop that, Let me talk about multi-threaded systems. Multi-threaded systems achieve parallelism by executing multiple lines of code typically by spawning many threads. Except Node is single-threaded and does not have parallelism. Then how does it achieve its asynchronous behaviour?

it simply uses non-blocking techniques. Instead of waiting for one line of code to finish executing, create a callback function that is invoked when the operations succeed or fail. Your code should do what it needs to do, then quickly hand control back over to the event loop so Node can work on something else.its asynchronous code that happens on the event loop, it does not have parallelism

What is Difference between multitasking and multi-threading

Concurrency and Event Loop

The event loop is generated only after every line of code is executed. It checks the stack and the task queue. If the queue is empty, takes the first thing on the queue and push on the stack(runs it). The event loop keeps spinning as long as there is something left to do. It only stops if either there is nothing else left to do or the program exits.

Node uses the event loop in c++ for scheduling work but executes application code in a js environment. C++ has access to the thread. it acts as central dispatch that routes requests to c++ and results back to Js. How information is shuffled between these two layers is the power of nodejs

Why is node single threaded yet faster than most multi-threaded environments?

it is event-driven All Js, v8 and the event loop run in one thread called the "main thread".

Event loop in action

  1. Everything set in the timer using methods such as setTimeout(), setInterval() is executed here.
  2. Execute pending os tasks and check which callback functions are ready to be called
  3. Pause execution Waiting for something to happen
  4. execute callback function is changes are detected
  5. determines that the program

Why Should I use Node?

I thought it is obvious by now, but I will tell you.

  1. Non-blocking I/O bound systems. Node is perfect for I/O bound programs. in most web applications and networking applications are i/o intensive. Request a server and wait for a response. While waiting, we can perform other tasks in node since its non-blocking and asynchronous.
  2. Develop faster because it has modules and reusable code from npm that you can tap into.
  3. clear and more consistent code-base because it uses JavaScript for both clients and server-side as well. Again its JavaScript so you can reuse code for the front and back-end as a well
  4. great for prototyping and agile development
  5. ecosystem of open source library i.e npm
  6. Asynchronous by default

What not to use node for

Majorly there are two types of systems: I/O bound vs CPU Bound. input/output systems are those that are limited by data access. Programs that request a service or a database read from or write to a file etc. Node thrives in such data-intensive applications.

CPU bound are programs that depend on the processing speed to perform such tasks. Mainly, crushing mathematical calculation.Do not use for CPU intensive tasks that require calculation such as processing images. Remember node is NOT multi-threaded

Do I have Node of my computer

check if node s installed by typing this on your terminal

node --version
or
node -V

you may have node or not. Check if you have the latest version on node.org website there are many ways of installing node on your computer 1. from nodejs website 2. from PPA 3. use nvm

What node is not

  1. node is NOT a programming language, its a runtime environment
  2. Node is NOT multi-threaded, is single-threaded and asynchronous
  3. Node is not a framework like Django, ASP.Net