Event Loop

효딩딩·2022년 8월 24일
0

이벤트 루프

  • 이벤트 루프는 태스크가 들어오길 기다렸다가 태스크가 들어오면 이를 처리하고, 처리할 태스크가 없는 경우엔 잠드는, 끊임없이 돌아가는 자바스크립트 내 루프입니다.

  • 자바스크립트 엔진이 돌아가는 알고리즘을 일반화하면 다음과 같습니다.

    	1. 처리해야 할 태스크가 있는 경우:
    		- 먼저 들어온 태스크부터 순차적으로 처리함
    	2. 처리해야 할 태스크가 없는 경우:
    		- 잠들어 있다가 새로운 태스크가 추가되면 다시 1로 돌아감

바로 이 알고리즘이 우리가 브라우저를 사용해 인터넷을 서핑할 때 돌아가는 알고리즘입니다. 이렇게 자바스크립트 엔진은 대부분의 시간 동안 아무런 일도 하지 않고 쉬고 있다가 스크립트나 핸들러, 이벤트가 활성화될 때만 돌아갑니다.

출처: https://velog.io/@thms200/Event-Loop-%EC%9D%B4%EB%B2%A4%ED%8A%B8-%EB%A3%A8%ED%94%84
https://ko.javascript.info/event-loop

(영문)

What is the Event Loop?

  • The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.

  • Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed. We'll explain this in further detail later in this topic.

Event Loop Explained

  • When Node.js starts, it initializes the event loop, processes the provided input script (or drops into the REPL, which is not covered in this document) which may make async API calls, schedule timers, or call process.nextTick(), then begins processing the event loop.

Source: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

profile
어제보다 나은 나의 코딩지식

0개의 댓글