perf(clock): use setImmediate for autoTick macrotask in Node
When called within an I/O cycle, `setImmediate` is generally faster because it is designed to execute immediately after the current I/O event completes, whereas `setTimeout(0)` gets placed in the timers queue and might be subject to delays. > The main advantage to using setImmediate() over setTimeout() is setImmediate() > will always be executed before any timers if scheduled within an I/O cycle, > independently of how many timers are present. * https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#setimmediate-vs-settimeout * https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#poll * https://nodejs.org/en/learn/asynchronous-work/understanding-setimmediate
This commit is contained in:
@@ -715,6 +715,20 @@ describe('Clock (acceptance)', function() {
|
||||
clock.uninstall();
|
||||
});
|
||||
|
||||
it('flushes microtask queue between macrotasks', async () => {
|
||||
const log = [];
|
||||
await new Promise(r => clock.setTimeout(r, 10)).then(() => {
|
||||
log.push(1);
|
||||
Promise.resolve().then(() => log.push(2));
|
||||
Promise.resolve().then(() => log.push(3));
|
||||
});
|
||||
await new Promise(r => clock.setTimeout(r, 10)).then(() => {
|
||||
log.push(4);
|
||||
Promise.resolve().then(() => log.push(5));
|
||||
});
|
||||
expect(log).toEqual([1, 2, 3, 4, 5]);
|
||||
});
|
||||
|
||||
it('can run setTimeouts/setIntervals asynchronously', function() {
|
||||
const recurring = jasmine.createSpy('recurring'),
|
||||
fn1 = jasmine.createSpy('fn1'),
|
||||
|
||||
Reference in New Issue
Block a user