# Node.js

## {% center %} Node.js {% endcenter %}

## What is Node.js?

It is a C++ program which takes in input a javascript code and executes it with a speciality that it doesn't block on I/O requests. It is called javaScript [runtime system](https://github.com/gomchikbhoka/blogs/tree/c21b011e5bbbf41649b1466e37a9e9d2a22defab/Program%20execution/runtimes.html) because it is a program that performs core or essential function for running javascript programs on system.

Node.js combined

* V8 JavaScript engine code (which was already written in c++)
* libuv, a *multi-platform support library* with a focus on evented I/O. It is a C library that implements the Node.js event loop and all of the asynchronous behaviors of the platform

You can develop any type of application on Node. e.g.

* a program which prints fibonacci series
* a chat application
* a compiler
* etc. etc.

  A subset of those will be server applications (like FTP server, video streaming server etc.), and then a subset of that will be web server applications. The most commonly used web server on Node is **Express**.

### I/O examples:

* database I/O
* console I/O
* socket I/O, like waiting for an http request on some port, sending reponse to an http request, making network request to some other server etc.

### Chrome V8 engine

The Chrome V8 engine is responsible for executing JavaScript code. It can actually be embedded into a C++ application which is what Node.js is at its core. The V8 engine takes in JavaScript as a string and executes it.

When Node.js c++ code starts executing,

* it initializes the event loop,&#x20;
* then processes the provided input script (using v8 engine code) which may make async API calls, schedule timers, or call process.nextTick() and
* then begins processing the event loop.

Important Resources:

* **(Must read)** [How node.js event loop works](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/)
* [Understanding the node.js event loop ](http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/)
* [Presentation on node.js](https://www.youtube.com/watch?v=M-sc73Y-zQA) by Ryan Dahl, creator of nodejs

### Apache vs Nginx/Node.js

Apache is multithreaded: it spawns a thread per request (or process, it depends on the conf). The overhead for each thread/process eats up memory as the number of concurrent connections increases and more threads are needed to serve multiple simulataneous clients. Nginx and Node.js are not multithreaded, because threads and processes carry a heavy memory cost. They are single-threaded, but event-based. This eliminates the overhead created by thousands of threads/processes by handling many connections in a single thread.

**How is node.js inherently memory efficient and faster when it still relies on threads internally ?**

* **memory efficient:** the actual threads are all contained at a fairly low level, and thus remain constrained in size and number. The catch is that Asynchronous I/O, when properly implemented at kernel level, does not use threads while performing async I/O operations. Instead the calling thread is released as soon as an I/O operation is started and a callback is executed when the I/O operation is finished and a thread is available for it. So node.js can run 50 concurrent requests with 50 I/O operations in (almost) parallel using just one thread if the async support for the I/O operations is properly implemented
* **faster:** OS-level "switching" via select() is faster than thread context swaps.
* [Stackoverflow discussion](https://stackoverflow.com/questions/3629784/how-is-node-js-inherently-faster-when-it-still-relies-on-threads-internally)

### Is it apt to compare Tomcat and Node.js ?

No, we should compare

* JVM vs. V8+Node
* Java vs. Javascript
* and Tomcat vs. Express

### Key points

* Node.js increased CPU utilization by using asynchronous I/O. CPU is able to do other work while I/O is happening.
* Node.js does not blow up memory like apache when there are huge number of connections concurrently because it is not creating a thread per connection.
* Node.js is fast as OS-level "switching" via select() is faster than thread context swaps.
* Node.js is not good for CPU intensive tasks. Better use something like apache for such tasks as they would take advantage of multiple cores/processors.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.gomchik.com/tech/program-execution/nodejs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
