DEV Community

Cover image for How NodeJS Interacts with the C/C++ language
Ben Matt, Jr.
Ben Matt, Jr.

Posted on

How NodeJS Interacts with the C/C++ language

Why node.js in the first place?

In the era of fast-growing technologies, Node.js plays a massive role in making enterprises more effective and productive. Here is an example:

In moving from a monolithic JAVA server architecture to Node.js, NETFLIX improved performance and reduced infrastructure costs.

  • They reduced the start-up time from 40 minutes to 1 minute.

  • They reduced the number of EC2 instances of Node compared with the legacy JAVA stack by 75% while serving the same number of subscribers at lower latencies.

Node.js is great at building fast network applications that scale. Using node.js, you'll be able to handle a vast number of connections simultaneously with high throughput, which equates to high scalability. Understanding how Node.js works under the hood will help you enhance the quality of your following Node.js applications.

Where do nodeJS take its superpower from?

Well, Node.js takes the best out of C and C++ languages as they are part of the fastest and most used low-level languages out there, but how are such interactions made possible?
For that, we need to examine both the V8

The v8 project:

The V8 project is a JAVASCRIPT engine created by Google to enable JAVASCRIPT code to run outside the browser, primarily built to run inside the Chrome browser. The cool thing is that the V8 engine is independent of the browser that runs it. This key feature enabled the rise of Node.js. The V8 engine was the technology built to be the engine that powered Node.js back in 2009. As the popularity of Node.js exploded, V8 became the engine that now powers an incredible amount of server-side code written in JAVASCRIPT.

The Node.js ecosystem is enormous thanks to the V8 engine that also powers desktop applications with projects like Electron.

The libuv project:

Libuv is the C library that implements the Node.js event loop, the asynchronous behaviors, and worker threads. It also gives Node access to the operating system, and as a result, actions like interacting with the file system and accessing device information are now possible.

The V8 engine and the Libuv library are with C and C++ programs. So the purpose of Node is to provide you with an API for accessing functionalities implemented inside V8 and Libuv. See the diagram below.

diagram

A closer look at this relation would look a bit different. The following diagram introduces a new concept called native extensions, more on that later.

diagram

Node.js interaction with other languages such as C and C++ is made possible by the AddOn and FFI(foreign function interface) technologies. Let have a look at them!

What are addOns?

in C++ that give Node.js access to specific functionalities. We can load them using the require() function just like any Node.js module out there! AddOns are a bridge between Javascript and C++ libraries.

Some of the common ways of implementing addons are Node-API, direct use of internal V8.

When not using Node-API, implementing addons is complicated, involving knowledge of several components and APIs

What is a foreign functions interface (FFI)?

Foreign Function Interfaces are ways to call into native code from a higher-level language. Among all the reasons for resorting to native code, performance is the most important.

πŸŽ‰πŸŽ‰πŸŽ‰ Thank you for reading the third part of this article! πŸŽ‰πŸŽ‰πŸŽ‰

And if you want more in depth knowledge about your favorite programming languages find me on Twitter as wellπŸ˜ƒ.

Top comments (0)