-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathpledge.test.js
85 lines (81 loc) · 2.17 KB
/
pledge.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as t from './testing.js'
import * as pledge from './pledge.js'
import * as promise from './promise.js'
/**
* @param {t.TestCase} _tc
*/
export const testPledgeCoroutine = async _tc => {
let called = false
const p = pledge.coroutine(function * () {
const y = pledge.wait(10).map(() => 42)
const num = yield y
console.log({ num })
t.assert(num === 42)
called = true
return 42
})
t.assert(!called)
await p.promise()
t.assert(called)
}
/**
* @param {t.TestCase} _tc
*/
export const testPledgeVsPromisePerformanceTimeout = async _tc => {
const iterations = 100
const waitTime = 0
await t.measureTimeAsync(`Awaiting ${iterations} callbacks (promise)`, async () => {
for (let i = 0; i < iterations; i++) {
await promise.wait(waitTime)
}
})
await t.measureTimeAsync(`Awaiting ${iterations} callbacks (pledge)`, () =>
pledge.coroutine(function * () {
for (let i = 0; i < iterations; i++) {
yield pledge.wait(waitTime)
}
}).promise()
)
}
/**
* @typedef {Promise<number> | number} MaybePromise
*/
/**
* @param {t.TestCase} _tc
*/
export const testPledgeVsPromisePerformanceResolved = async _tc => {
const iterations = 100
t.measureTime(`Awaiting ${iterations} callbacks (only iterate)`, () => {
for (let i = 0; i < iterations; i++) { /* nop */ }
})
await t.measureTimeAsync(`Awaiting ${iterations} callbacks (promise)`, async () => {
for (let i = 0; i < iterations; i++) {
await promise.resolve(0)
}
})
await t.measureTimeAsync(`Awaiting ${iterations} callbacks (await, no resolve)`, async () => {
for (let i = 0; i < iterations; i++) {
/**
* @type {Promise<number> | number}
*/
const x = 0
await x
}
})
await t.measureTimeAsync(`Awaiting ${iterations} callbacks (pledge)`, () =>
pledge.coroutine(function * () {
for (let i = 0; i < iterations; i++) {
yield 0
}
}).promise()
)
t.measureTime(`Awaiting ${iterations} callbacks (pledge, manual wrap)`, () => {
/**
* @type {pledge.Pledge<number>}
*/
let val = 0
for (let i = 0; i < iterations; i++) {
val = pledge.map(val, _v => 0)
}
})
}