Skip to content

Commit caf442e

Browse files
bughitMaledong
authored andcommitted
remove the outdated Deduplication section from 'event-loop-timers-and-nexttick.md' (#2158)
Deduplication section appears to be describing how things worked prior to nodejs/node#22842, so is no longer correct.
1 parent 0d17c5f commit caf442e

File tree

6 files changed

+89
-161
lines changed

6 files changed

+89
-161
lines changed

locale/en/docs/guides/event-loop-timers-and-nexttick.md

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -398,56 +398,6 @@ To get around this, the `'listening'` event is queued in a `nextTick()`
398398
to allow the script to run to completion. This allows the user to set
399399
any event handlers they want.
400400

401-
### Deduplication
402-
403-
For the `timers` and `check` phases, there is a single transition
404-
between C to JavaScript for multiple immediates and timers. This deduplication
405-
is a form of optimization, which may produce some unexpected side effects.
406-
Take this code snippet as an example:
407-
408-
```js
409-
// dedup.js
410-
const foo = [1, 2];
411-
const bar = ['a', 'b'];
412-
413-
foo.forEach(num => {
414-
setImmediate(() => {
415-
console.log('setImmediate', num);
416-
bar.forEach(char => {
417-
process.nextTick(() => {
418-
console.log('process.nextTick', char);
419-
});
420-
});
421-
});
422-
});
423-
```
424-
```bash
425-
$ node dedup.js
426-
setImmediate 1
427-
setImmediate 2
428-
process.nextTick a
429-
process.nextTick b
430-
process.nextTick a
431-
process.nextTick b
432-
```
433-
434-
The main thread adds two `setImmediate()` events, which when processed
435-
will add two `process.nextTick()` events. When the event loop reaches
436-
the `check` phase, it sees that there are currently two events created by
437-
`setImmediate()`. The first event is grabbed and processed, which prints
438-
and adds two events to the `nextTickQueue`.
439-
440-
Because of deduplication, the event loop does not transition back to the
441-
C/C++ layer to check if there are items in the `nextTickQueue` immediately. It
442-
instead continues to process any remaining `setImmediate()` events, of which
443-
one currently remains. After processing this event, two more events are
444-
added to the `nextTickQueue` for a total of four events.
445-
446-
At this point, all previously added `setImmediate()` events have been processed.
447-
The `nextTickQueue` is now checked, and events are processed in FIFO order. When
448-
this `nextTickQueue` is emptied, the event loop considers all operations to have
449-
been completed for the current phase and transitions to the next phase.
450-
451401
## `process.nextTick()` vs `setImmediate()`
452402

453403
We have two calls that are similar as far as users are concerned, but

locale/fa/docs/guides/event-loop-timers-and-nexttick.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ emitted via `process.nextTick()`.
218218
`setImmediate()` and `setTimeout()` are similar, but behave in different
219219
ways depending on when they are called.
220220

221-
* `setImmediate()` is designed to execute a script once the current
222-
**poll** phase completes.
221+
* `setImmediate()` is designed to execute a script once the
222+
current **poll** phase completes.
223223
* `setTimeout()` schedules a script to be run after a minimum threshold
224224
in ms has elapsed.
225225

@@ -293,8 +293,11 @@ within an I/O cycle, independently of how many timers are present.
293293
You may have noticed that `process.nextTick()` was not displayed in the
294294
diagram, even though it's a part of the asynchronous API. This is because
295295
`process.nextTick()` is not technically part of the event loop. Instead,
296-
the `nextTickQueue` will be processed after the current operation
297-
completes, regardless of the current phase of the event loop.
296+
the `nextTickQueue` will be processed after the current operation is
297+
completed, regardless of the current phase of the event loop. Here,
298+
an *operation* is defined as a transition from the
299+
underlying C/C++ handler, and handling the JavaScript that needs to be
300+
executed.
298301

299302
Looking back at our diagram, any time you call `process.nextTick()` in a
300303
given phase, all callbacks passed to `process.nextTick()` will be

locale/it/docs/guides/event-loop-timers-and-nexttick.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ emitted via `process.nextTick()`.
215215

216216
## `setImmediate()` vs `setTimeout()`
217217

218-
`setImmediate` and `setTimeout()` are similar, but behave in different
218+
`setImmediate()` and `setTimeout()` are similar, but behave in different
219219
ways depending on when they are called.
220220

221-
* `setImmediate()` is designed to execute a script once the current
222-
**poll** phase completes.
221+
* `setImmediate()` is designed to execute a script once the
222+
current **poll** phase completes.
223223
* `setTimeout()` schedules a script to be run after a minimum threshold
224224
in ms has elapsed.
225225

@@ -293,8 +293,11 @@ within an I/O cycle, independently of how many timers are present.
293293
You may have noticed that `process.nextTick()` was not displayed in the
294294
diagram, even though it's a part of the asynchronous API. This is because
295295
`process.nextTick()` is not technically part of the event loop. Instead,
296-
the `nextTickQueue` will be processed after the current operation
297-
completes, regardless of the current phase of the event loop.
296+
the `nextTickQueue` will be processed after the current operation is
297+
completed, regardless of the current phase of the event loop. Here,
298+
an *operation* is defined as a transition from the
299+
underlying C/C++ handler, and handling the JavaScript that needs to be
300+
executed.
298301

299302
Looking back at our diagram, any time you call `process.nextTick()` in a
300303
given phase, all callbacks passed to `process.nextTick()` will be

locale/ja/docs/guides/event-loop-timers-and-nexttick.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ emitted via `process.nextTick()`.
218218
`setImmediate()` and `setTimeout()` are similar, but behave in different
219219
ways depending on when they are called.
220220

221-
* `setImmediate()` is designed to execute a script once the current
222-
**poll** phase completes.
221+
* `setImmediate()` is designed to execute a script once the
222+
current **poll** phase completes.
223223
* `setTimeout()` schedules a script to be run after a minimum threshold
224224
in ms has elapsed.
225225

@@ -293,8 +293,11 @@ within an I/O cycle, independently of how many timers are present.
293293
You may have noticed that `process.nextTick()` was not displayed in the
294294
diagram, even though it's a part of the asynchronous API. This is because
295295
`process.nextTick()` is not technically part of the event loop. Instead,
296-
the `nextTickQueue` will be processed after the current operation
297-
completes, regardless of the current phase of the event loop.
296+
the `nextTickQueue` will be processed after the current operation is
297+
completed, regardless of the current phase of the event loop. Here,
298+
an *operation* is defined as a transition from the
299+
underlying C/C++ handler, and handling the JavaScript that needs to be
300+
executed.
298301

299302
Looking back at our diagram, any time you call `process.nextTick()` in a
300303
given phase, all callbacks passed to `process.nextTick()` will be

locale/ko/docs/guides/event-loop-timers-and-nexttick.md

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ The following diagram shows a simplified overview of the event loop's
4343
order of operations.
4444
4545
```
46-
┌───────────────────────┐
47-
┌─>│ timers │
48-
│ └──────────────────────┘
49-
│ ┌──────────────────────┐
50-
│ │ pending callbacks │
51-
│ └──────────────────────┘
52-
│ ┌──────────────────────┐
53-
│ │ idle, prepare │
54-
│ └──────────────────────┘ ┌───────────────┐
55-
│ ┌──────────────────────┐ │ incoming: │
56-
│ │ poll │<─────┤ connections, │
57-
│ └──────────────────────┘ │ data, etc. │
58-
│ ┌──────────────────────┐ └───────────────┘
59-
│ │ check │
60-
│ └──────────────────────┘
61-
│ ┌──────────────────────┐
62-
└──┤ close callbacks │
63-
└───────────────────────┘
46+
┌───────────────────────────
47+
┌─>│ timers
48+
│ └─────────────┬─────────────┘
49+
│ ┌─────────────┴─────────────┐
50+
│ │ pending callbacks
51+
│ └─────────────┬─────────────┘
52+
│ ┌─────────────┴─────────────┐
53+
│ │ idle, prepare
54+
│ └─────────────┬─────────────┘ ┌───────────────┐
55+
│ ┌─────────────┴─────────────┐ │ incoming: │
56+
│ │ poll │<─────┤ connections, │
57+
│ └─────────────┬─────────────┘ │ data, etc. │
58+
│ ┌─────────────┴─────────────┐ └───────────────┘
59+
│ │ check
60+
│ └─────────────┬─────────────┘
61+
│ ┌─────────────┴─────────────┐
62+
└──┤ close callbacks
63+
└───────────────────────────
6464
```
6565
6666
*note: each box will be referred to as a "phase" of the event loop.*
@@ -76,24 +76,24 @@ Node.js를 시작할 때 이벤트 루프를 초기화하고 제공된 입력
7676
아래 다이어그램은 이벤트 루프의 작업 순서의 간단한 개요를 보여줍니다.
7777

7878
```
79-
┌───────────────────────┐
80-
┌─>│ timers │
81-
│ └──────────────────────┘
82-
│ ┌──────────────────────┐
83-
│ │ pending callbacks │
84-
│ └──────────────────────┘
85-
│ ┌──────────────────────┐
86-
│ │ idle, prepare │
87-
│ └──────────────────────┘ ┌───────────────┐
88-
│ ┌──────────────────────┐ │ incoming: │
89-
│ │ poll │<─────┤ connections, │
90-
│ └──────────────────────┘ │ data, etc. │
91-
│ ┌──────────────────────┐ └───────────────┘
92-
│ │ check │
93-
│ └──────────────────────┘
94-
│ ┌──────────────────────┐
95-
└──┤ close callbacks │
96-
└───────────────────────┘
79+
┌───────────────────────────
80+
┌─>│ timers
81+
│ └─────────────┬─────────────┘
82+
│ ┌─────────────┴─────────────┐
83+
│ │ pending callbacks
84+
│ └─────────────┬─────────────┘
85+
│ ┌─────────────┴─────────────┐
86+
│ │ idle, prepare
87+
│ └─────────────┬─────────────┘ ┌───────────────┐
88+
│ ┌─────────────┴─────────────┐ │ incoming: │
89+
│ │ poll │<─────┤ connections, │
90+
│ └─────────────┬─────────────┘ │ data, etc. │
91+
│ ┌─────────────┴─────────────┐ └───────────────┘
92+
│ │ check
93+
│ └─────────────┬─────────────┘
94+
│ ┌─────────────┴─────────────┐
95+
└──┤ close callbacks
96+
└───────────────────────────
9797
```
9898

9999
*note: 각 박스는 이벤트 루프의 "단계"를 의미합니다.*
@@ -142,9 +142,12 @@ _**NOTE:** 윈도우와 Unix/Linux 구현체간에 약간의 차이가 있지만
142142
143143
* **timers**: this phase executes callbacks scheduled by `setTimeout()`
144144
and `setInterval()`.
145-
* **pending callbacks**: executes I/O callbacks deferred to the next loop iteration.
145+
* **pending callbacks**: executes I/O callbacks deferred to the next loop
146+
iteration.
146147
* **idle, prepare**: only used internally.
147-
* **poll**: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and `setImmediate()`); node will block here when appropriate.
148+
* **poll**: retrieve new I/O events; execute I/O related callbacks (almost
149+
all with the exception of close callbacks, the ones scheduled by timers,
150+
and `setImmediate()`); node will block here when appropriate.
148151
* **check**: `setImmediate()` callbacks are invoked here.
149152
* **close callbacks**: some close callbacks, e.g. `socket.on('close', ...)`.
150153
@@ -309,7 +312,7 @@ error. This will be queued to execute in the **pending callbacks** phase.
309312
310313
The **poll** phase has two main functions:
311314
312-
1. Executing scripts for timers whose threshold has elapsed, then
315+
1. Calculating how long it should block and poll for I/O, then
313316
2. Processing events in the **poll** queue.
314317
315318
When the event loop enters the **poll** phase _and there are no timers
@@ -325,7 +328,7 @@ is reached.
325328

326329
**poll** 단계는 두 가지 주요 기능을 가집니다.
327330

328-
1. 임계 값이 지난 타이머의 스크립트를 실행합니다. 그다음
331+
1. I/O를 얼마나 오래 블록하고 폴링해야 하는지 계산합니다. 그 다음
329332
2. **poll** 큐에 있는 이벤트를 처리합니다.
330333

331334
이벤트 루프가 **poll** 단계에 진입하고 _스케줄링된 타이머가 없을 때_
@@ -413,11 +416,11 @@ emitted via `process.nextTick()`.
413416
<!--
414417
## `setImmediate()` vs `setTimeout()`
415418
416-
`setImmediate` and `setTimeout()` are similar, but behave in different
419+
`setImmediate()` and `setTimeout()` are similar, but behave in different
417420
ways depending on when they are called.
418421
419-
* `setImmediate()` is designed to execute a script once the current
420-
**poll** phase completes.
422+
* `setImmediate()` is designed to execute a script once the
423+
current **poll** phase completes.
421424
* `setTimeout()` schedules a script to be run after a minimum threshold
422425
in ms has elapsed.
423426
@@ -560,8 +563,11 @@ timeout
560563
You may have noticed that `process.nextTick()` was not displayed in the
561564
diagram, even though it's a part of the asynchronous API. This is because
562565
`process.nextTick()` is not technically part of the event loop. Instead,
563-
the `nextTickQueue` will be processed after the current operation
564-
completes, regardless of the current phase of the event loop.
566+
the `nextTickQueue` will be processed after the current operation is
567+
completed, regardless of the current phase of the event loop. Here,
568+
an *operation* is defined as a transition from the
569+
underlying C/C++ handler, and handling the JavaScript that needs to be
570+
executed.
565571
566572
Looking back at our diagram, any time you call `process.nextTick()` in a
567573
given phase, all callbacks passed to `process.nextTick()` will be
@@ -577,8 +583,9 @@ from reaching the **poll** phase.
577583

578584
`process.nextTick()`이 비동기 API에 속해있지만, 다이어그램에는 표시되지 않은 것을
579585
눈치챘을 겁니다. 이는 `process.nextTick()`이 기술적으로는 이벤트 루프의 일부가
580-
아니기 때문입니다. 대신 `process.nextTick()`은 이벤트 루프의 현재 단계와 관계없이
581-
현재 작업이 완료된 후에 처리될 것입니다.
586+
아니기 때문입니다. 대신 `nextTickQueue`는 이벤트 루프의 현재 단계와 관계없이
587+
현재 작업이 완료된 후에 처리될 것입니다. 여기에서 *작업*이란 기저의 C/C++
588+
핸들러에서 전환하는 것, 또 실행되어야 하는 JavaScript를 처리하는 것을 말합니다.
582589

583590
다이어그램을 다시 보겠습니다. 해당 단계에서 `process.nextTick()`을 호출하면
584591
`process.nextTick()`에 전달한 모든 콜백은 언제나 이벤트 루프를 계속 진행하기
@@ -730,12 +737,12 @@ const server = net.createServer(() => {}).listen(8080);
730737
server.on('listening', () => {});
731738
```
732739
733-
When only a port is passed the port is bound immediately. So the
734-
`'listening'` callback could be called immediately. Problem is that the
735-
`.on('listening')` will not have been set by that time.
740+
When only a port is passed, the port is bound immediately. So, the
741+
`'listening'` callback could be called immediately. The problem is that the
742+
`.on('listening')` callback will not have been set by that time.
736743
737-
To get around this the `'listening'` event is queued in a `nextTick()`
738-
to allow the script to run to completion. Which allows the user to set
744+
To get around this, the `'listening'` event is queued in a `nextTick()`
745+
to allow the script to run to completion. This allows the user to set
739746
any event handlers they want.
740747
-->
741748

@@ -762,7 +769,7 @@ server.on('listening', () => {});
762769
```
763770

764771
포트만 전달하면 포트가 바로 바인딩 됩니다. 그래서 `'listening'` 콜백이 바로 호출될 수 있습니다.
765-
문제는 `.on('listening')` 이때 설정되지 않는다는 것입니다.
772+
문제는 `.on('listening')` 콜백이 이때 설정되지 않는다는 것입니다.
766773

767774
이를 피하려면 `'listening'` 이벤트를 `nextTick()`으로 큐에 넣어서 스크립트가 완료될 때까지
768775
실행되도록 할 수 있습니다. 이를 통해 어떤 이벤트 핸들러라도 설정하도록 할 수 있습니다.
@@ -778,10 +785,10 @@ their names are confusing.
778785
event loop
779786
780787
In essence, the names should be swapped. `process.nextTick()` fires more
781-
immediately than `setImmediate()` but this is an artifact of the past
788+
immediately than `setImmediate()`, but this is an artifact of the past
782789
which is unlikely to change. Making this switch would break a large
783790
percentage of the packages on npm. Every day more new modules are being
784-
added, which mean every day we wait, more potential breakages occur.
791+
added, which means every day we wait, more potential breakages occur.
785792
While they are confusing, the names themselves won't change.
786793
787794
*We recommend developers use `setImmediate()` in all cases because it's
@@ -847,9 +854,9 @@ server.on('listening', () => { });
847854

848855
<!--
849856
Say that `listen()` is run at the beginning of the event loop, but the
850-
listening callback is placed in a `setImmediate()`. Now, unless a
851-
hostname is passed binding to the port will happen immediately. Now for
852-
the event loop to proceed it must hit the **poll** phase, which means
857+
listening callback is placed in a `setImmediate()`. Unless a
858+
hostname is passed, binding to the port will happen immediately. For
859+
the event loop to proceed, it must hit the **poll** phase, which means
853860
there is a non-zero chance that a connection could have been received
854861
allowing the connection event to be fired before the listening event.
855862
@@ -875,8 +882,8 @@ myEmitter.on('event', () => {
875882
-->
876883

877884
`listen()`이 이벤트 루프 시작 부분에서 실행되었지만, listening 콜백은 `setImmediate()`
878-
있습니다. 이제 바인딩할 호스트네임을 전달하지 않는 한 포트는 즉시 적용될 것입니다. 이제 이벤트 루프를
879-
진행하려면 **poll** 단계에 도달해야 하는데 이 말은 listening 이벤트 전에 connection 이벤트가
885+
있습니다. 바인딩할 호스트네임을 전달하지 않는 한 포트는 즉시 적용될 것입니다. 이벤트 루프를
886+
진행하려면 **poll** 단계에 도달해야 하는데, 이 말은 listening 이벤트 전에 connection 이벤트가
880887
발생하도록 해서 연결을 받을 가능성이 있다는 것입니다.
881888

882889
또 다른 예제는 `EventEmitter`를 상속받고 생성자 내에서 이벤트를 호출하고자 하는

0 commit comments

Comments
 (0)