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 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,
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
Presentation on node.js 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.
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.
Last updated