- Is a client-side scripting programming language
- 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
- It is synchronous, you have to wait for one line of code to execute before you can execute the next.
Node is characterized by: 1. Events-driven. Everything happening in Node is a reaction to an event 2. Non-blocking i/o.
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
- Everything set in the timer using methods such as setTimeout(), setInterval() is executed here.
- Execute pending os tasks and check which callback functions are ready to be called
- Pause execution Waiting for something to happen
- execute callback function is changes are detected
- determines that the program
Why Should I use Node?
I thought it is obvious by now, but I will tell you.
- 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.
- Develop faster because it has modules and reusable code from npm that you can tap into.
- great for prototyping and agile development
- ecosystem of open source library i.e npm
- 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
- node is NOT a programming language, its a runtime environment
- Node is NOT multi-threaded, is single-threaded and asynchronous
- Node is not a framework like Django, ASP.Net