Skip to main content

Command Palette

Search for a command to run...

[Node] What is Node.js?

How it runs: Non-Blocking I/O, Event Loop, and Single Thread

Updated
[Node] What is Node.js?

What is Node.js?

  • JS runtime built with Chrome V8 JS Engine

    • V8 Engine: Made it possible for JS to run even without a browser, whereas previously it could only run within a browser.
  • Uses an event-driven, non-blocking I/O model for lightweight and efficient performance

  • npm, the package ecosystem of Node is a very large open-source library environment

Why Node.js?

  1. Non-blocking I/O

    1. In traditional blocking I/O, when a function is called within a program, it waits for the called function to complete its task before proceeding, which can result in delays. However, non-blocking I/O allows other tasks to be carried out simultaneously while a function is still executing, providing advantages such as parallel programming and greater control over the execution flow.

      • Blocking - Linear programming, easy to code, but has limited control. It waits until I/O is completed, blocking other tasks from being processed.

      • Non-blocking - Parallel programming, harder to code, but provides more control. It does not wait for system calls to complete, allowing the user process to handle other tasks.

    2. What is the difference between Non-blocking I/O & Async?

      1. If control is passed, non-blocking allows other code to be executed, making asynchronous processing possible. On the other hand, if control is not passed, blocking occurs and asynchronous processing is not possible.

      2. Therefore, JavaScript is implemented to allow functions to be executed asynchronously due to its non-blocking model, although commands are executed sequentially.

      3. In conclusion, JavaScript adopts an Async + Non-blocking model, which allows for the invocation of the next code even before the current executing code has finished.

  2. Single Thread

    1. Single Thread refers to using only one thread, which can handle only one task at a time.

      โ†’ Thread refers to a unit that utilizes the CPU or processor when a program is running.

    2. JavaScript itself is single-threaded, meaning it has only one main thread for executing JavaScript code. However, Web APIs, such as DOM manipulation and AJAX requests, are typically implemented in separate threads by browsers, allowing for concurrent processing.

      In fact, Single Thread offers many advantages. Compared to a system that creates threads for each connection request, it avoids the overhead of thread creation and maintenance, making more efficient use of the same computing resources.

      Furthermore, having only one thread means that there are no issues with concurrent access to shared resources, such as Race conditions, among threads. Since there are no shared resources, situations, where multiple threads cause problems due to a single error, do not occur.

      Node.js, on the other hand, prefers to create new processes for scalability. Processes are independent even if there are multiple processes, so the probability of all processes being terminated due to one process causing an issue is relatively low.

      Due to these characteristics, server scalability based on a load of connection requests is very flexible with Node.js. In other words, Node.js has a language philosophy that is particularly focused on web servers.

      Despite the advantages of Single Thread, its inherent issues become apparent when compared to the computational efficiency of Multi-threading. This is why Node.js is not efficient for CPU-intensive tasks. However, methods to overcome these weaknesses are provided, and there are ways to add additional threads in practice!

      It is important to remember that Node.js is designed based on the Single Thread philosophy.

  3. Event Loop

    1. The event loop is what allows Node.js to perform non-blocking I/O operations โ€” despite the fact that JavaScript is single-threaded

    2. The event loop is a feature in JavaScript that efficiently handles tasks to overcome the limitations of the single-threaded model.

JavaScript Runtime

img

  • Call stack: Functions are stacked and executed in order

  • Web API: APIs that the browser supports for asynchronous works

  • Task/Event Queue: Store async-related functions (like a callback, async/await, Promise, Generator) from Web APIs

  • Event Loop: When the call stack is empty, the event loop picks tasks from the task queue and pushes them onto the call stack for execution.

How those are connected to each other

Situation: JavaScript is single-threaded, meaning it has only one call stack. When time-consuming operations are pushed onto the call stack, other code cannot be executed, resulting in blocking situations.

Solution:

  1. Time-consuming operations such as events (DOM, AJAX, etc.) are performed asynchronously through Web APIs, which do not block the main thread

  2. These events have async-related functions attached to them, which are sequentially queued in the event/task queue after event processing is completed in the background.

  3. These queued events are dequeued by the event loop and pushed onto the call stack when it is empty.

REPL in Node.js

  • It reads the code input, stores it in memory, evaluates the value, and prints the output. This process is repeated in a loop until a specific signal is received.

  • It can be considered a development environment suitable for quick testing and debugging due to the immediate verification of the results of the code input, such as syntax testing or testing.

  • Available on Chrome Developer Tools

  • Available on the terminal by typing node

More from this blog

[์ฝ”ํ…Œ] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

https://school.programmers.co.kr/learn/courses/30/lessons/42891 ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์— ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋Š” ๋ฌธ์ œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋จน๋Š” ์‹œ๊ฐ„์ด ์งง์€ ์Œ์‹๋ถ€ํ„ฐ ํ์—์„œ ๋นผ๊ธฐ import heapq # ์šฐ์„ ์ˆœ์œ„ํ ํ™œ์šฉ: food_time์ด ์งง์€ ์Œ์‹๋ถ€ํ„ฐ ์‚ญ์ œ def solution(food_times, k): if sum(food_times) <= k: return -1 ...

Apr 4, 2024
[์ฝ”ํ…Œ] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

[์ฝ”ํ…Œ] ์—ฌํ–‰๊ฒฝ๋กœ

๐Ÿ’ก [์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€] ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง„ ๋น„ํ–‰๊ธฐ ํ‹ฐ์ผ“์„ ํ†ตํ•ด ๋ชจ๋“  ํ‹ฐ์ผ“์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ๊ณตํ•ญ์„ ๋ฐฉ๋ฌธ ์ˆœ์„œ ๊ตฌํ•˜๊ธฐ (๋‹จ, ์—ฌ๋Ÿฌ ๊ณตํ•ญ์„ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ์ด ๋น ๋ฅธ ๊ณตํ•ญ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•œ๋‹ค.) ํ‹€๋ ธ๋˜ ์ฝ”๋“œ from collections import defaultdict def dfs(graph, route, depart): if graph[depart]: connected = graph[depart][0] ...

Feb 26, 2024
[์ฝ”ํ…Œ] ์—ฌํ–‰๊ฒฝ๋กœ

siwon.log

161 posts