-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1-tinybench-bench.mjs
More file actions
51 lines (43 loc) · 1.26 KB
/
1-tinybench-bench.mjs
File metadata and controls
51 lines (43 loc) · 1.26 KB
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
import { Bench } from 'tinybench'
import { strictEqual } from 'node:assert'
import loadThenables from './loader.mjs'
const LONG_ARRAY_SIZE = 100_000
const bench = new Bench()
const thenables = await loadThenables()
bench.add('Promise.all (short)', async () => {
const [a, b] = await Promise.all([Promise.resolve(1), Promise.resolve(2)])
strictEqual(a, 1)
strictEqual(b, 2)
})
bench.add('Promise.all (long)', 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.add(thenable.name + ' (short)', 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)
})
bench.add(thenable.name + ' (long)', 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 bench.warmup()
await bench.run()
console.table(bench.table())