Author: Chloe Yan
From the official website: ‘Node.js is an open-source and cross-platform JavaScript runtime environment. Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser.‘
Before understanding what Node.js is, we first need to understand the relationship between JavaScript and the parsing engine.
JavaScript & Parsing Engine
The relationship between JavaScript code and the parsing engine is similar to that between a script and a director. JavaScript code is the text that describes the storylines (functional logic) of a program, while the parsing engine is the director responsible for interpreting the text. Once the director understands the script, they guide the actors, prop managers, and other crew members (representing parts of the computer, such as the CPU, memory, etc.) to perform their roles.
Figure 1. Comparative Relationship Diagram: JavaScript and Parsing Engine vs. Director and Script
Thus, JavaScript code needs to be processed by the parsing engine before the computer can understand and execute it.
Why browsers can run JavaScript code
JavaScript can run in browsers because browsers come with built-in parsing engines. Different browsers use different parsing engines: for example, the Chrome browser uses the V8 engine , Firefox uses the OdinMonkey engine, and Safari uses the JSCore engine. Compared to OdinMonkey and JSCore, the V8 engine’s Just-In-Time (JIT) compilation is more powerful, utilizing inline caching techniques to accelerate repetitive operations. V8 also features a more efficient garbage collection mechanism and better cross-platform support. Therefore, among these, the V8 engine is considered the best in terms of performance.
Figure 2 JavaScript Execution Flow in Chrome Browser
What Node.js is
Node.js separates the V8 engine from the Chrome browser, allowing it to run outside of the browser environment. Node.js does not have a graphical user interface (GUI) like typical desktop applications. Instead, it operates as a command-line application, relying on text-based input and output. This makes it particularly suited for server-side code development and other backend processing tasks.
JavaScript is commonly used on the front-end in browsers to create interactive user interfaces. The browser serves as the front-end runtime environment, handling tasks like rendering HTML, CSS, and JavaScript, and directly displaying content to users. Node.js, on the other hand, acts as the back-end runtime environment for JavaScript, handling server-side tasks such as interacting with databases and managing server operations. Node.js makes it possible to use JavaScript for developing back-end applications.
Figure 3 Execution Flow in Node.js
As a JavaScript runtime environment, Node.js provides fundamental features and APIs such as fs, path, and http. Based on these core functionalities, many tools and frameworks have been designed, making Node.js even more convenient to use. For example, the Express framework can be used to quickly build web applications, while the Electron framework is suitable for creating cross-platform desktop applications.
How Node.js Works: Event-Driven, Non-blocking I/O, and Single-Threaded Architecture
Node.js uses a single-threaded model to handle all requests. All I/O operations (such as file reading or database queries) are executed via an event-driven mechanism. Non-blocking I/O means that when handling these operations, Node.js does not block the thread, allowing it to process other requests simultaneously. This approach reduces the need for a thread pool, avoiding the overhead of creating new threads for each request. As a result, Node.js can efficiently handle a large number of concurrent requests, making it well-suited for real-time applications such as stock trading systems or big data application development.
Figure 4 How Node.js works
Event-Driven: In a movie shooting scenario, when an actor is getting their makeup done, the director does not wait nearby for the makeup to finish. Instead, they might check the progress of the set arrangement and props preparation or discuss the shooting plan with the cinematographer. Once the makeup is done, the makeup artist informs the director, who then returns to review the actor’s appearance and guide the next step of the shoot. Here, “makeup is done” is the event, and the director is the event listener, waiting for the event and responding to it.
In Node.js, event-driven programming is a model where program execution is triggered by events, such as mouse clicks, file read completions, or keyboard inputs.
Single-Threaded Architecture: In this film crew, there is only one director responsible for all tasks, such as directing the actors’ performances, checking the set arrangements, and finalizing the shooting plans. Since a single director handles all decisions, it avoids conflicts of opinion or redundant instructions from multiple sources.
In a multi-threaded model, each concurrent request is handled by a different thread, and each thread has its own memory space, which leads to higher memory consumption. In Node.js, a single-threaded architecture is used to handle all requests, reducing the need for thread pools.
Non-blocking I/O: In a movie shooting scenario, the director does not get stuck waiting for the actor to finish their makeup. Instead, the director checks the progress of set arrangements and prop preparations.
Similarly, in non-blocking I/O, the program does not freeze the entire system while waiting for a task to complete, it can continue processing other tasks.
Summary
Node.js is an open-source, cross-platform JavaScript runtime environment that separates the V8 engine from Chrome, allowing it to run as a command-line application outside the browser. It enables JavaScript to be used for server-side application development. Node.js features a single-threaded, event-driven, non-blocking I/O architecture, allowing for efficient handling of multiple events simultaneously. Node.js processes concurrent requests without the need for thread pools, optimizing performance for tasks like file reading, database queries, and data-intensive applications. This makes it ideal for real-time applications such as stock trading platforms.
References
- JavaScriptCore – WebKit Documentation (no date). Available at: https://docs.webkit.org/Deep%20Dive/JSC/JavaScriptCore.html (Accessed: 24 January 2025).
- Node.js — Blog (2025). Available at: https://nodejs.org/en/blog (Accessed: 24 January 2025).
- Node.js — Introduction to Node.js (no date). Available at: https://nodejs.org/en/learn/getting-started/introduction-to-nodejs (Accessed: 24 January 2025).
- Node.js — The V8 JavaScript Engine (no date). Available at: https://nodejs.org/en/learn/getting-started/the-v8-javascript-engine (Accessed: 24 January 2025).
- Node.js Tutorial for Beginners: Learn Node in 1 Hour (no date). Available at: https://www.youtube.com/watch?v=TlB_eWDSMt4 (Accessed: 24 January 2025).
- NodeJS中的事件驱动程序实现原理解析Node.js 使用事件驱动模型,当web server接收到请求,就把它关闭 – 掘金 (no date). Available at: https://juejin.cn/post/6875601541013700621 (Accessed: 24 January 2025).
- ‘WebKit/WebKit’ (2025). The WebKit Open Source Project. Available at: https://github.com/WebKit/WebKit (Accessed: 24 January 2025).
- 事件驱动架构设计 (2022) QIN. Available at: https://www.qin.news/event-driven/ (Accessed: 24 January 2025).
- Node.js Tutorial for Beginners: Learn Node in 1 Hour (2019) https://youtu.be/TlB_eWDSMt4?feature=shared (Accessed: 24 January 2025)