You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document is a developer-oriented description of what event loop does. It tries to be readable, and might skip over edge cases for clarity.
45
-
46
-
## Why is the event loop interesting?
47
-
48
-
Event loop is in charge.
49
-
All the application code is scheduled for execution by the event loop.
50
-
51
-
Event loop is interesing because it influences performance and correctness of your app.
52
-
53
-
If your event loop takes more than 16ms, animations might skip a frame. If your code is split into multiple event handlers, Promise callbacks, timeouts, observers, in what order will the code execute? Do you really need to `setTimeout(_=>{doIt();}, 1)` because things were not executing in the right order?
44
+
*[Discussion on event order](https://github.com/w3c/pointerevents/issues/9)
54
45
55
-
Understanding the event loop will help you understand when your code gets executed, and its performance implications.
46
+
This document is a developer-oriented description of what event loop does. It tries to be readable, and might skip over edge cases for clarity.
56
47
57
48
## Event loop description
58
49
@@ -68,21 +59,33 @@ This is what the spec says:
68
59
},
69
60
70
61
microtaskQueue: [
71
-
// microtasks are:
72
-
// - completed promises
73
62
],
74
63
64
+
nextTask: function() {
65
+
// Spec says:
66
+
// "Select the oldest task on one of the event loop's task queues"
67
+
// Which gives browser implementers freedom to implement
68
+
// task queues
69
+
for (let q of taskQueues)
70
+
if (q.length > 0)
71
+
return q.shift();
72
+
return null;
73
+
},
74
+
75
75
executeMicrotasks: function() {
76
+
if (scriptExecuting)
77
+
return;
76
78
let microtasks = this.microtaskQueue;
77
79
this.microtaskQueue = [];
78
80
for (let t of microtasks)
79
81
t.execute();
80
82
},
81
-
needsPaint: function() {
82
-
return vSyncTime() && needsDomRepaint();
83
+
84
+
needsRendering: function() {
85
+
return vSyncTime() && needsDomRerender();
83
86
},
84
87
85
-
paintPipeline: function() {
88
+
render: function() {
86
89
resizeSteps();
87
90
scrollSteps();
88
91
mediaQuerySteps();
@@ -93,8 +96,10 @@ This is what the spec says:
93
96
94
97
intersectionObserverSteps();
95
98
96
-
updateStyle();
97
-
updateLayout();
99
+
while (resizeObserverSteps()) {
100
+
updateStyle();
101
+
updateLayout();
102
+
}
98
103
paint();
99
104
}
100
105
}
@@ -105,62 +110,11 @@ This is what the spec says:
105
110
task.execute();
106
111
}
107
112
eventLoop.executeMicrotasks();
108
-
if (eventLoop.needsPaint())
109
-
eventLoop.paintPipeline();
110
-
}
111
-
112
-
This is what really happens in Chrome:
113
-
114
-
eventLoop = {
115
-
taskQueues: {
116
-
events: [], // UI events from native GUI framework
117
-
parser: [], // HTML parser
118
-
callbacks: [], // setTimeout, requestIdleTask
119
-
resources: [], // image loading
120
-
domManipulation[]
121
-
},
122
-
123
-
microtasks: [
124
-
// microtasks are:
125
-
// - completed promises
126
-
],
127
-
128
-
executeMicrotasks: function() {
129
-
let microtasks = this.microtasks;
130
-
this.microtasks = [];
131
-
for (let t of microtasks)
132
-
t.execute();
133
-
},
134
-
needsPaint: function() {
135
-
return vSyncTime() && needsDomRepaint();
136
-
},
137
-
138
-
paintPipeline: function() {
139
-
mediaQuerySteps();
140
-
resizeSteps();
141
-
scrollSteps();
142
-
cssAnimationSteps();
143
-
fullscreenRenderingSteps();
144
-
145
-
animationFrameCallbackSteps();
146
-
147
-
intersectionObserverSteps();
148
-
149
-
updateStyle();
150
-
updateLayout();
151
-
paint();
152
-
}
113
+
if (eventLoop.needsRendering())
114
+
eventLoop.render();
153
115
}
154
116
155
-
while(true) {
156
-
task = eventLoop.nextTask();
157
-
if (task) {
158
-
task.execute();
159
-
}
160
-
eventLoop.executeMicrotasks();
161
-
if (eventLoop.needsPaint())
162
-
eventLoop.paintPipeline();
163
-
}
117
+
## Chrome implementation of the event loop
164
118
165
119
### Events
166
120
@@ -184,31 +138,40 @@ Also manages requestAnimationFrame requests
0 commit comments