DEV Community

Cover image for What is Node.js?
Fabrizio Lallo for Node Doctors

Posted on • Edited on

What is Node.js?

JavaScript had the reputation of being a toy language. A tool to add interactivity to web pages. For a long time, its use cases were limited to the browser, but the creation of Node.js completely changed its role in the modern web development world.

Node.js allows us to run JavaScript outside the browser, and out of nowhere, a limited language became the favorite companion for thousands of developers working on small and big projects.

Its vibrant community created many tools that power many applications on servers, desktops, and embedded systems.

  • Node.js is commonly used with Express or Fastify to power complex backend services.
  • Electron package Node.js and Chrome to create cross-platform desktop applications. It is used in production to power programmers’ favorite editor VSCode and messaging applications like Microsoft Teams.
  • Embedded systems can leverage low.js to develop applications.

Why Node.js?

Before Node.js

Contrary to popular belief, Node wasn’t invented to allow JavaScript outside the browser. The latter was a positive outcome. It was developed to create more performant web servers.

Ten years ago, handling multiple user connections involved the creation of a new thread, an independent unit of execution within the web server process. This methodology implied two problems:

  • If you perform a database query, it blocks the entire thread from executing other operations until it receives the result.

blocking execution

  • Handling concurrent connections with associated threads requires high memory consumption and increased difficulty in managing synchronization between them.

The Node.js way

JavaScript and Node.js solve the problem of threads' high memory usage and synchronization by using a single thread paired with an event loop mechanism.

non-blocking execution

In the code snippet above, Node.js can handle concurrent connections and run other pieces of code because the database query execution is delegated to the event loop. The database operation has a callback function which is run when the event loop notifies the single thread that the query is completed.

single thread running the query

Node.js internal overview

To understand Node.js, we need to understand V8.

V8 is a C++ program that runs JavaScript code and converts it into instructions understandable by the computer microprocessor. V8 is included in Google Chrome and other browsers to run our websites JavaScript and enable our rich web experience.

The V8 engine is not limited to modern browsers but can also be embedded into any C++ program and extended with more functionalities. Not by chance, Node.js is a C++ program that integrates the V8 engine and other libraries to enrich JavaScript with new features:

  • libuv, the C++ library responsible for the event loop, TCP and UDP connection sockets, DNS resolution, file system operations, data encryption, etc.
  • C++ bindings are responsible for invoking C++ code when running Node.js-specific JavaScript code.
  • The JavaScript core API.

node.js internals overview

Conclusion

Node.js clever design and event loop has empowered thousands of developers and products to create web servers capable of handling thousands of concurrent connections without the hassle of threads’ high memory consumption and synchronization. Node.js event loop capabilities are not limited to database queries and concurrent connections, but they are also used in production to process large files, encrypt/decrypt data, perform network requests, etc.

You will explore in more detail the event loop in the following article.

If you liked the article, follow us on Twitter @fabrizio.lallo and @AndrewHu368

Top comments (0)