- Google V8 Homepage
- Intoduction To v8
- Source Code on Github
- Understanding V8 Engine Compilation
- Inside The V8 Engine
Starting a Node Process
To start a new node process, all you have to do is type node at the command prompt. We assume you have already installed node on your machine.
Your first Node application
You can now write your first Node application. Type the following in the terminal and hit enter.
> console.log('Hello from NodeJS!'); Hello from NodeJS! undefined >
Node Console vs Browser Console
So we can see that we are outputting the same result when running our tiny little program. Well, it turns out that even though each environment looks like it has a similar console, behind the scenes they are providing different tools to the developer.
In the browser, the
window object on the client side.
The Node environment has a similar concept to window, however in node it is called
global. If you type
global at the Node command line, you’ll see a lot of information about what the
global object stores.
Exiting the Node terminal
To exit the node console, you can type the following.
> process.exit(0); C:node>
Now, you will be back at your machine’s command prompt and not the node runtime process. You also have the option to hit control C twice like so.
> (To exit, press ^C again or type .exit) > C:node>
global in Node is similar to
window in the browser,
process in Node is similar to
document in the browser. If you’re coming from a front end back ground those two objects are likely familiar to you. Now when learning about Node, you can kind of apply those same concepts to
What Makes NodeJS So Great?
If you visit the Node homepage, you’ll see a sentence that states the following: “Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.” Let’s talk about that for a second. Node uses non blocking IO or Input Output. What this means is that while one user is requesting a resource from a Node application, a different user can request an entirely different resource without causing any wait. Have a look at this diagram to see what we mean by blocking vs non blocking.
On the left hand side, we can see how a blocking application works. While we are fetching from the database, which is an IO operation, the application can not do anything else. This means our machine is wasting cycles waiting for the database to respond and can’t even do something simple like adding two numbers.
On the right, we have a non blocking scenario. When the application makes a call to fetch a user from the database, it does not have to wait for this process to finish before beginning other tasks. In fact, right after the first request is made, another different request is made to fetch a different user. In the second scenario, the application does not need to wait. The key takeaway is that in a non blocking approach, the application can run more than one operation at the same time. This leads to faster performance.
NodeJs is single threaded, or in other words, a node application runs on one single thread. This is not a problem since Node is non blocking. In a blocking context, an application can still handle more than one request, but it requires more cpu and ram resources to support building more and more threads. This is what makes Node very efficient. It uses the lease amount of resources for the most amount of performance.
- How NodeJs Single Threading Works
- Understanding Event Loop in NodeJs
- Overview of Blocking vs Non-Blocking
- Blocking and non-blocking calls in Node
Something else we should talk about is another sentence from the NodeJS homepage: “Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.” Node has a dedicated package manager called NPM. When they say it is the largest ecosystem in the world, they are not kidding. According to modulecounts.com, npm is showing over 600,000 packages managed by the npm system! By comparison, we can look at this chart and see that the other package managers like Maven Central, nuget, packagist, PyPI, and rubygems are lagging.
img credit: http://www.modulecounts.com/
What We Learned About Node
- Developers can use Node.js to build fast and scalable networking applications. It’s a great option for building RESTful services.
- Node.js applications are single-threaded. In other words, a single thread is used to serve all clients.
- Applications in Node.js are asynchronous or non-blocking by default. That means when I/O operations such as accessing the file system or the network happen, the thread doesn’t wait (or block) for the result of the operation. It is released to serve other clients.
- The asynchronous architecture makes Node.js perfect for building I/O-intensive applications.
- Node.js is not a good choice for CPU-intensive applications, such as a video encoding service.
- Browser environment objects such as window or the document object do not exist in Node.js. Instead, there are other objects that are not available in browsers, such as objects for working with the file system, network, operating system, and more.
What Is NodeJS Summary