-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2-mitata-bench.mjs
55 lines (46 loc) · 1.32 KB
/
2-mitata-bench.mjs
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
import { run, bench, group, baseline } from 'mitata'
import { strictEqual } from 'node:assert'
import loadThenables from './loader.mjs'
const LONG_ARRAY_SIZE = 100_000
const thenables = await loadThenables()
group('Short array', () => {
baseline('Promise.all', async () => {
const [a, b] = await Promise.all([Promise.resolve(1), Promise.resolve(2)])
strictEqual(a, 1)
strictEqual(b, 2)
})
for (const thenable of thenables) {
bench(thenable.name, async () => {
// eslint-disable-next-line
Array.prototype.then = thenable;
const [a, b] = await [Promise.resolve(1), Promise.resolve(2)]
strictEqual(a, 1)
strictEqual(b, 2)
})
}
})
group('Long array', () => {
baseline('Promise.all', async () => {
const promises = []
for (let i = 1; i <= LONG_ARRAY_SIZE; i++) {
promises.push(Promise.resolve(i))
}
const [a, b] = await Promise.all(promises)
strictEqual(a, 1)
strictEqual(b, 2)
})
for (const thenable of thenables) {
bench(thenable.name, async () => {
// eslint-disable-next-line
Array.prototype.then = thenable;
const array = []
for (let i = 1; i <= LONG_ARRAY_SIZE; i++) {
array.push(Promise.resolve(i))
}
const [a, b] = await array
strictEqual(a, 1)
strictEqual(b, 2)
})
}
})
await run()