-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.js
136 lines (121 loc) · 3.7 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const bench = require('benchmark');
const suite = new bench.Suite('Primitives');
const { Decode, decode } = require('./index');
class User {}
class Tree {}
// Taken from https://stackoverflow.com/a/43053803
const cartesian = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));
const support = {
object: Decode.object({
value: Decode.integer,
}),
lazyInteger: Decode.lazy(() => Decode.integer),
listInteger: Decode.many(Decode.number),
UserDecoder: Decode.instance(User, {
id: Decode.field('id', Decode.integer),
name: Decode.field('name', Decode.string),
}),
TreeDecoder: Decode.instance(Tree, {
value: Decode.field('value', Decode.number),
child: Decode.field(
'child',
Decode.optional(
null,
Decode.lazy(() => support.TreeDecoder),
),
),
}),
makeTree: depth => {
let root = { value: Math.random() };
let tree = root;
while (depth-- > 0) {
tree.child = { value: Math.random() };
tree = tree.child;
}
return root;
},
makeList: (size, value) => {
const arr = new Array(size);
while (size-- > 0) arr[size] = value;
return arr;
},
// We want to measure the performance of optionalField itself as best as we
// can, not that of its child decoder.
optionalField: Decode.optionalField('value', undefined, Decode.unknown),
};
suite
.add('Decode.unknown', () => {
return decode(Decode.unknown, 42);
})
.add('Decode.string', () => {
return decode(Decode.string, 'value');
})
.add('Decode.number', () => {
return decode(Decode.number, 42.3);
})
.add('Decode.integer', () => {
return decode(Decode.integer, 42);
})
.add('Decode.bool', () => {
return decode(Decode.bool, true);
})
.add('Decode.lazy integer', () => {
return decode(support.lazyInteger, 42);
})
.add('Decode.object simple', () => {
return decode(support.object, 42);
})
.add('Decode.instance User model', () => {
return decode(support.UserDecoder, {
id: 42,
name: 'Yo Bo',
});
})
.add('Decode.optionalField (field present)', () => {
return decode(support.optionalField, {
value: 42,
});
})
.add('Decode.optionalField (field absent)', () => {
return decode(support.optionalField, {});
});
[10, 100, 1000].forEach(depth => {
const tree = support.makeTree(depth);
suite.add(`Decode tree (depth: ${depth})`, () => {
return decode(support.TreeDecoder, tree);
});
});
cartesian(
[10, 1000, 100000],
[
{ name: 'Decode.integer', decoder: Decode.integer, data: 42 },
{ name: 'Decode.string', decoder: Decode.string, data: '42' },
{
name: 'User model',
decoder: support.UserDecoder,
data: { id: 42, name: 'Name' },
},
],
).forEach(entry => {
const size = entry[0];
const info = entry[1];
const list = support.makeList(size, info.data);
const decoder = Decode.many(info.decoder);
suite.add(`Decode.many (size: ${size}, decoder: ${info.name})`, () => {
return decode(decoder, list);
});
});
const args = process.argv.slice(2);
const filter = args.length > 0
? args[0].split(',').map(str => str.trim().toLowerCase())
: [];
suite.filter(bench => {
if (filter.length === 0) {
return true;
}
const name = bench.name.toLowerCase();
return filter.some(f => name.includes(f));
}).on('cycle', cycle => {
const msg = cycle.target.toString();
console.log(msg);
}).run();