{"id":1289,"date":"2025-01-25T07:33:19","date_gmt":"2025-01-25T12:33:19","guid":{"rendered":"https:\/\/itp.nyu.edu\/networks\/?page_id=1289"},"modified":"2025-01-25T07:33:19","modified_gmt":"2025-01-25T12:33:19","slug":"node-js","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/networks\/explanations\/node-js\/","title":{"rendered":"Node.js"},"content":{"rendered":"\n<p><em>Author: Chloe Yan<\/em><\/p>\n\n\n\n<p><em>From the <strong>o<\/strong>fficial website: &#8216;<\/em><a href=\"https:\/\/nodejs.org\/en\/learn\/getting-started\/introduction-to-nodejs\"><em>Node.js<\/em><\/a><em> 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.<\/em>&#8216;<\/p>\n\n\n\n<p>Before understanding what Node.js is, we first need to understand the relationship between JavaScript and the parsing engine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JavaScript_Parsing_Engine\"><\/span><strong>JavaScript &amp; Parsing Engine<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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)<strong> <\/strong>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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdQtrTck5DpTJh9w-I8Hw_FKdiTz0ytTlpYj4BF6r0qOBpORA2bURMpA5h6bTNhBkVHxQi_qn3l7ws1NtogoZX45JCbPGAVtDMqNYVxWEiTx7pHWw4axdv87FJ75jkRuAXXSoDp?key=hJHC0aDi1xr8aQ-Lub7zTrbN\" alt=\"A diagram showing the comparative Relationship Diagram: JavaScript and Parsing Engine vs. Director and Script\"\/><\/figure>\n\n\n\n<p><em>Figure 1. Comparative Relationship Diagram: JavaScript and Parsing Engine vs. Director and Script<\/em><\/p>\n\n\n\n<p>Thus, JavaScript code needs to be processed by the parsing engine before the computer can understand and execute it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_browsers_can_run_JavaScript_code\"><\/span><strong>Why browsers can run JavaScript code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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 <a href=\"https:\/\/v8.dev\/\">V8 engine<\/a> , Firefox uses the <a href=\"https:\/\/www.nocleansinging.com\/2013\/06\/28\/odinmonkey-amon-amarth-inspires-firefox\/\">OdinMonkey<\/a> engine, and Safari uses the <a href=\"https:\/\/docs.webkit.org\/Deep%20Dive\/JSC\/JavaScriptCore.html\">JSCore<\/a> engine. Compared to OdinMonkey and JSCore, the V8 engine\u2019s <a href=\"https:\/\/nodejs.org\/en\/learn\/getting-started\/the-v8-javascript-engine#compilation\">Just-In-Time (JIT) compilation<\/a> is more powerful, utilizing inline caching techniques to accelerate repetitive operations. V8 also features a more efficient <a href=\"https:\/\/medium.com\/@mmoshikoo\/garbage-collector-in-v8-engine-1c582399837\">garbage collection mechanism <\/a>and better cross-platform support. Therefore, among these, the V8 engine is considered the best in terms of performance.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXe_9OngkUFW4l7TjD4I5wp0oJYq08-PVWfE2ZpJi6O8D07Ida6wlKRmnhfrcwq3Ql9iZBw3uGYjhka1VtpsmkYKHCBQzWwsjLavYiwlr_Rt0Co6zeAbEKHng4m7AmyXzPNwKmyv?key=hJHC0aDi1xr8aQ-Lub7zTrbN\" alt=\"\"\/><\/figure>\n\n\n\n<p><em>Figure 2 JavaScript Execution Flow in Chrome Browser<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Nodejs_is\"><\/span><strong>What Node.js is<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfScWSHFep7-MJ7pELyvKNQkkYxv9T7eWJxhBEjHmAdmqrgYEG1iJf_CjwCDNJRpJ_T05kOTZnxK_S_wF45YLU1Vax5MvMeK8u0azpzU08Zpz0_cNriKUqoEll5j18XkdmqFwcp8g?key=hJHC0aDi1xr8aQ-Lub7zTrbN\" alt=\"A diagram showing Execution Flow in Node.js\n\"\/><\/figure>\n\n\n\n<p><em>Figure 3 Execution Flow in Node.js<\/em><\/p>\n\n\n\n<p>As a JavaScript runtime environment, Node.js provides fundamental features and<a href=\"https:\/\/nodejs.org\/docs\/v22.11.0\/api\/n-api.html#napicreateexternal_buffer\"> APIs<\/a> such as<a href=\"https:\/\/nodejs.org\/docs\/v22.11.0\/api\/fs.html#fswritevsyncfd-buffers-position\"> fs<\/a>,<a href=\"https:\/\/nodejs.org\/docs\/v22.11.0\/api\/path.html#path\"> path<\/a>, and<a href=\"https:\/\/nodejs.org\/docs\/v22.11.0\/api\/http.html#http\"> http<\/a>. Based on these core functionalities, many tools and frameworks have been designed, making Node.js even more convenient to use. For example, the<a href=\"https:\/\/expressjs.com\/\"> Express<\/a> framework can be used to quickly build web applications, while the<a href=\"https:\/\/en.wikipedia.org\/wiki\/Electron_(software_framework)\"> Electron<\/a> framework is suitable for creating cross-platform desktop applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Nodejs_Works_Event-Driven_Non-blocking_IO_and_Single-Threaded_Architecture\"><\/span><strong>How Node.js Works: <\/strong><strong>Event-Driven, Non-blocking I\/O, and Single-Threaded Architecture<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXc83Hb2-WgrxQT_PNwmgeZh-SqKEhROijzYPRHlBiNqCmiFPkYWtHTObDixPkqTAWreSPuMvq5OZr6GcyseJxj75q0wS-UUkf_ZQ2D54dKgSljZ_A8YQN9wlxHNTwGIC73Qdhbcqw?key=hJHC0aDi1xr8aQ-Lub7zTrbN\" alt=\"A diagram of node.js' execution model, showing the relationship between events, the event handler, threads, and the components of the operating system.\"\/><\/figure>\n\n\n\n<p><em>Figure 4 How Node.js works<\/em><\/p>\n\n\n\n<p><strong>Event-Driven<\/strong>: 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\u2019s appearance and guide the next step of the shoot. Here, \u201cmakeup is done\u201d is the event, and the director is the event listener, waiting for the event and responding to it.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Single-Threaded Architecture:<\/strong> In this film crew, there is only one director responsible for all tasks, such as directing the actors\u2019 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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p><strong>Non-blocking I\/O<\/strong>: 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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span><strong>Summary<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"References\"><\/span><strong>Reference<\/strong>s<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>JavaScriptCore &#8211; WebKit Documentation<\/em> (no date). Available at: <a href=\"https:\/\/docs.webkit.org\/Deep%20Dive\/JSC\/JavaScriptCore.html\">https:\/\/docs.webkit.org\/Deep%20Dive\/JSC\/JavaScriptCore.html<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li><em>Node.js \u2014 Blog<\/em> (2025). Available at: <a href=\"https:\/\/nodejs.org\/en\/blog\">https:\/\/nodejs.org\/en\/blog<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li><em>Node.js \u2014 Introduction to Node.js<\/em> (no date). Available at: <a href=\"https:\/\/nodejs.org\/en\/learn\/getting-started\/introduction-to-nodejs\">https:\/\/nodejs.org\/en\/learn\/getting-started\/introduction-to-nodejs<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li><em>Node.js \u2014 The V8 JavaScript Engine<\/em> (no date). Available at: <a href=\"https:\/\/nodejs.org\/en\/learn\/getting-started\/the-v8-javascript-engine\">https:\/\/nodejs.org\/en\/learn\/getting-started\/the-v8-javascript-engine<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li><em>Node.js Tutorial for Beginners: Learn Node in 1 Hour<\/em> (no date). Available at: <a href=\"https:\/\/www.youtube.com\/watch?v=TlB_eWDSMt4\">https:\/\/www.youtube.com\/watch?v=TlB_eWDSMt4<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li><em>NodeJS\u4e2d\u7684\u4e8b\u4ef6\u9a71\u52a8\u7a0b\u5e8f\u5b9e\u73b0\u539f\u7406\u89e3\u6790Node.js \u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\uff0c\u5f53web server\u63a5\u6536\u5230\u8bf7\u6c42\uff0c\u5c31\u628a\u5b83\u5173\u95ed &#8211; \u6398\u91d1<\/em> (no date). Available at: <a href=\"https:\/\/juejin.cn\/post\/6875601541013700621\">https:\/\/juejin.cn\/post\/6875601541013700621<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li>\u2018WebKit\/WebKit\u2019 (2025). The WebKit Open Source Project. Available at: <a href=\"https:\/\/github.com\/WebKit\/WebKit\">https:\/\/github.com\/WebKit\/WebKit<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li>\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u8bbe\u8ba1 (2022) <em>QIN<\/em>. Available at: <a href=\"https:\/\/www.qin.news\/event-driven\/\">https:\/\/www.qin.news\/event-driven\/<\/a> (Accessed: 24 January 2025).<\/li>\n\n\n\n<li>Node.js Tutorial for Beginners: Learn Node in 1 Hour (2019) <a href=\"https:\/\/youtu.be\/TlB_eWDSMt4?feature=shared\">https:\/\/youtu.be\/TlB_eWDSMt4?feature=shared<\/a> (Accessed: 24 January 2025)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Author: Chloe Yan From the official website: &#8216;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.&#8216; Before understanding what Node.js is, we first need to understand the relationship between JavaScript and the parsing engine. JavaScript &amp; Parsing Engine The relationship &hellip; <a href=\"https:\/\/itp.nyu.edu\/networks\/explanations\/node-js\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Node.js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":60,"featured_media":0,"parent":19,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1289","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/1289"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/users\/60"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/comments?post=1289"}],"version-history":[{"count":3,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/1289\/revisions"}],"predecessor-version":[{"id":1302,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/1289\/revisions\/1302"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/pages\/19"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/networks\/wp-json\/wp\/v2\/media?parent=1289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}