-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
43 lines (39 loc) 路 1004 Bytes
/
index.ts
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
import { suite } from 'npm:@marais/bench';
import DataLoader from 'npm:dataloader';
import * as dldr from '../mod.ts';
import * as dldrCache from '../cache/mod.ts';
const loadFn: dldr.LoadFn<any, string> = async (keys: string[]) => Promise.resolve(keys);
await suite<any[]>(
{
dldr: () => {
return (keys) => Promise.all(keys.map((key) => dldr.load(loadFn, key)));
},
'dldr/cache': () => {
const cache = new Map();
return (keys) =>
Promise.all(
keys.map((key) => dldrCache.load(loadFn, cache, key)),
);
},
dataloader: () => {
const loader = new DataLoader(loadFn as any, {
cache: false,
});
return (keys) => loader.loadMany(keys);
},
'dataloader/cache': () => {
const loader = new DataLoader(loadFn as any, {
cache: true,
});
return (keys) => loader.loadMany(keys);
},
},
(run) => {
const keys = ['a', 'b', 'c', 'a'];
run(
undefined,
() => keys,
(results) => results.every((result, i) => result === keys[i]),
);
},
);