-
Notifications
You must be signed in to change notification settings - Fork 8
perf: uses setImmediate() implementation from tiny-set-immediate #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -51,28 +51,27 @@ describe('TaskScheduler', () => { | |||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| describe('a task scheduler when setImmediate exists', () => { | ||||||
RubenVerborgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| describe('a task scheduler using an externally-provided setImmediate() implementation', () => { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const backups = {}; | ||||||
| let setImmediate = sinon.spy(); | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just let it default here; or spy on |
||||||
|
|
||||||
| beforeEach(() => { | ||||||
| backups.setTimeout = global.setTimeout; | ||||||
| backups.queueMicrotask = global.queueMicrotask; | ||||||
| backups.setImmediate = global.setImmediate; | ||||||
| global.setImmediate = sinon.spy(); | ||||||
| global.setTimeout = sinon.spy(); | ||||||
| global.queueMicrotask = sinon.spy(); | ||||||
| setImmediate = sinon.spy(); | ||||||
| }); | ||||||
|
|
||||||
| after(() => { | ||||||
| global.setTimeout = backups.setTimeout; | ||||||
| global.queueMicrotask = backups.queueMicrotask; | ||||||
| global.setImmediate = backups.setImmediate; | ||||||
| delete global.window; | ||||||
| }); | ||||||
|
|
||||||
| it('alternates between setImmediate and queueMicrotask', () => { | ||||||
| // Perform 100 calls | ||||||
| const taskScheduler = createTaskScheduler(); | ||||||
| const taskScheduler = createTaskScheduler(setImmediate); | ||||||
| const tasks = []; | ||||||
| for (let i = 0; i < 100; i++) { | ||||||
| tasks[i] = sinon.spy(); | ||||||
|
|
@@ -83,11 +82,11 @@ describe('TaskScheduler', () => { | |||||
| expect(global.setTimeout).to.have.callCount(0); | ||||||
|
|
||||||
| // 99 calls should have gone to queueMicrotask, 1 to setImmediate | ||||||
| expect(global.setImmediate).to.have.callCount(1); | ||||||
| expect(setImmediate).to.have.callCount(1); | ||||||
| expect(global.queueMicrotask).to.have.callCount(99); | ||||||
|
|
||||||
| // When the setImmediate callback is invoked, the final call should to through | ||||||
| global.setImmediate.getCall(0).callback(); | ||||||
| setImmediate.getCall(0).callback(); | ||||||
| expect(global.queueMicrotask).to.have.callCount(100); | ||||||
|
|
||||||
| // Verify all microtasks were scheduled | ||||||
|
|
@@ -97,7 +96,7 @@ describe('TaskScheduler', () => { | |||||
|
|
||||||
| it('waits for setImmediate to finish before calling queueMicrotask', () => { | ||||||
| // Perform 150 calls | ||||||
| const taskScheduler = createTaskScheduler(); | ||||||
| const taskScheduler = createTaskScheduler(setImmediate); | ||||||
| const tasks = []; | ||||||
| for (let i = 0; i < 150; i++) { | ||||||
| tasks[i] = sinon.spy(); | ||||||
|
|
@@ -108,11 +107,11 @@ describe('TaskScheduler', () => { | |||||
| expect(global.setTimeout).to.have.callCount(0); | ||||||
|
|
||||||
| // 99 calls should have gone to queueMicrotask, 1 to setImmediate | ||||||
| expect(global.setImmediate).to.have.callCount(1); | ||||||
| expect(setImmediate).to.have.callCount(1); | ||||||
| expect(global.queueMicrotask).to.have.callCount(99); | ||||||
|
|
||||||
| // When the setImmediate callback is invoked, the final call should to through | ||||||
| global.setImmediate.getCall(0).callback(); | ||||||
| setImmediate.getCall(0).callback(); | ||||||
| expect(global.queueMicrotask).to.have.callCount(150); | ||||||
|
|
||||||
| // Verify all microtasks were scheduled | ||||||
|
|
@@ -121,48 +120,6 @@ describe('TaskScheduler', () => { | |||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| describe('a task scheduler when setImmediate does not exist', () => { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe keep this test, but repurpose for custom implementation? I like that the test verifies the alternating behavior in another way. |
||||||
| const backups = {}; | ||||||
|
|
||||||
| before(() => { | ||||||
| backups.setTimeout = global.setTimeout; | ||||||
| backups.queueMicrotask = global.queueMicrotask; | ||||||
| backups.setImmediate = global.setImmediate; | ||||||
| global.setImmediate = undefined; | ||||||
| global.setTimeout = sinon.spy(); | ||||||
| global.queueMicrotask = sinon.spy(); | ||||||
| }); | ||||||
|
|
||||||
| after(() => { | ||||||
| global.setTimeout = backups.setTimeout; | ||||||
| global.queueMicrotask = backups.queueMicrotask; | ||||||
| global.setImmediate = backups.setImmediate; | ||||||
| delete global.window; | ||||||
| }); | ||||||
|
|
||||||
| it('alternates between setTimeout and queueMicrotask', () => { | ||||||
| // Perform 100 calls | ||||||
| const taskScheduler = createTaskScheduler(); | ||||||
| const tasks = []; | ||||||
| for (let i = 0; i < 100; i++) { | ||||||
| tasks[i] = sinon.spy(); | ||||||
| taskScheduler(tasks[i]); | ||||||
| } | ||||||
|
|
||||||
| // 99 calls should have gone to queueMicrotask, 1 to setTimeout | ||||||
| expect(global.setTimeout).to.have.callCount(1); | ||||||
| expect(global.queueMicrotask).to.have.callCount(99); | ||||||
|
|
||||||
| // When the setTimeout callback is invoked, the final call should to through | ||||||
| global.setTimeout.getCall(0).args[0](); | ||||||
| expect(global.queueMicrotask).to.have.callCount(100); | ||||||
|
|
||||||
| // Verify all microtasks were scheduled | ||||||
| for (let i = 0; i < 100; i++) | ||||||
| expect(global.queueMicrotask).to.have.been.calledWith(tasks[i]); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| describe('a task scheduler when queueMicrotask is unavailable', () => { | ||||||
| const backups = {}; | ||||||
|
|
||||||
|
|
@@ -176,7 +133,7 @@ describe('TaskScheduler', () => { | |||||
| }); | ||||||
|
|
||||||
| it('can schedule a task', done => { | ||||||
| const taskScheduler = createTaskScheduler(); | ||||||
| const taskScheduler = createTaskScheduler(setImmediate); | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest to leave unchanged |
||||||
| taskScheduler(done); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.