Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 7ee8bcd

Browse files
JiaLiPassionmhevery
authored andcommitted
feat(performance): reuse microTaskQueue native promise (#874)
1 parent eed776c commit 7ee8bcd

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Diff for: lib/zone.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1251,14 +1251,20 @@ const Zone: ZoneType = (function(global: any) {
12511251
const symbolThen = __symbol__('then');
12521252
let _microTaskQueue: Task[] = [];
12531253
let _isDrainingMicrotaskQueue: boolean = false;
1254+
let nativeMicroTaskQueuePromise: any;
12541255

12551256
function scheduleMicroTask(task?: MicroTask) {
12561257
// if we are not running in any task, and there has not been anything scheduled
12571258
// we must bootstrap the initial task creation by manually scheduling the drain
12581259
if (_numberOfNestedTaskFrames === 0 && _microTaskQueue.length === 0) {
12591260
// We are not running in Task, so we need to kickstart the microtask queue.
1260-
if (global[symbolPromise]) {
1261-
global[symbolPromise].resolve(0)[symbolThen](drainMicroTaskQueue);
1261+
if (!nativeMicroTaskQueuePromise) {
1262+
if (global[symbolPromise]) {
1263+
nativeMicroTaskQueuePromise = global[symbolPromise].resolve(0);
1264+
}
1265+
}
1266+
if (nativeMicroTaskQueuePromise) {
1267+
nativeMicroTaskQueuePromise[symbolThen](drainMicroTaskQueue);
12621268
} else {
12631269
global[symbolSetTimeout](drainMicroTaskQueue, 0);
12641270
}

0 commit comments

Comments
 (0)