-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
120 lines (106 loc) · 3.33 KB
/
bench.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { suite, add, cycle, complete } from 'benny';
import { pIter, pIterEnumerated, pIterSettled } from 'p-iterate';
import frstcmfrstsvd from 'frstcmfrstsvd';
// Disclaimer: These benchmarks are largely irrelevant, since in the intended
// use case, the input promises' async operations themselves will take the
// lion's share of the time. The benchmarks are mostly for practice and
// curiosity.
function* range(start, stop) {
while (start < stop) {
yield start++;
}
}
const oneHundredResolved = [...range(0, 100)].map((n) => Promise.resolve(n));
const oneThousandResolved = [...range(0, 1000)].map((n) => Promise.resolve(n));
const consumers = [pIter, pIterEnumerated, pIterSettled, frstcmfrstsvd];
(async () => {
// Unfortunately, eslint doesn't support top-level await yet
function consumeAll(consumer, source) {
return add(consumer.name, async () => {
// eslint-disable-next-line no-unused-vars, no-empty
for await (const value of consumer(source)) {
}
});
}
await suite(
'process an array of 100 resolved promises',
...consumers.map((consumer) => consumeAll(consumer, oneHundredResolved)),
add('Promise.all', async () => {
await Promise.all(oneHundredResolved);
}),
add('Promise.allSettled', async () => {
await Promise.allSettled(oneHundredResolved);
}),
cycle(),
complete()
);
function consumeN(consumer, source, count) {
return add(consumer.name, async () => {
// eslint-disable-next-line no-unused-vars
for await (const value of consumer(oneHundredResolved)) {
if (count-- === 0) {
break;
}
}
});
}
await suite(
'process one value from an array of 100 resolved promises',
...consumers.map((consumer) => consumeN(consumer, oneHundredResolved, 1)),
add('Promise.race()', async () => {
await Promise.race(oneHundredResolved);
}),
cycle(),
complete()
);
await suite(
'process an array of 1000 resolved promises',
...consumers.map((consumer) => consumeAll(consumer, oneThousandResolved)),
add('Promise.all', async () => {
await Promise.all(oneThousandResolved);
}),
add('Promise.allSettled', async () => {
await Promise.allSettled(oneThousandResolved);
}),
cycle(),
complete()
);
const lastResolved = [
...[...range(0, 99)].map(() => new Promise(() => {})),
Promise.resolve(99),
];
await suite(
'process one value from an array of 99 pending and 1 resolved promises',
...consumers.map((consumer) => consumeN(consumer, lastResolved, 1)),
add('Promise.race()', async () => {
await Promise.race(lastResolved);
}),
cycle(),
complete()
);
function* generateResolved(count) {
let index = 0;
while (index < count) {
yield Promise.resolve(index++);
}
}
function addAwait(name, awaiter, source) {
return add(name, async () => {
await awaiter(source);
});
}
await suite(
'process a generator of 100 resolved promises',
...[pIter, pIterSettled, pIterEnumerated].map((consumer) =>
consumeAll(consumer, generateResolved(100))
),
addAwait('Promise.all()', Promise.all.bind(Promise), generateResolved(100)),
addAwait(
'Promise.allSettled()',
Promise.allSettled.bind(Promise),
generateResolved(100)
),
cycle(),
complete()
);
})();