forked from gcanti/io-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
109 lines (96 loc) · 1.9 KB
/
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
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
import * as Benchmark from 'benchmark'
import * as t from '../src'
/*
space-object (good) x 476,424 ops/sec ±0.45% (92 runs sampled)
space-object (bad) x 434,563 ops/sec ±0.58% (87 runs sampled)
*/
const suite = new Benchmark.Suite()
const Vector = t.tuple([t.number, t.number, t.number])
const Asteroid = t.type({
type: t.literal('asteroid'),
location: Vector,
mass: t.number
})
const Planet = t.type({
type: t.literal('planet'),
location: Vector,
mass: t.number,
population: t.number,
habitable: t.boolean
})
const Rank = t.keyof({
captain: null,
'first mate': null,
officer: null,
ensign: null
})
const CrewMember = t.type({
name: t.string,
age: t.number,
rank: Rank,
home: Planet
})
const Ship = t.type({
type: t.literal('ship'),
location: Vector,
mass: t.number,
name: t.string,
crew: t.array(CrewMember)
})
const T = t.union([Asteroid, Planet, Ship])
const good = {
type: 'ship',
location: [1, 2, 3],
mass: 4,
name: 'foo',
crew: [
{
name: 'bar',
age: 44,
rank: 'captain',
home: {
type: 'planet',
location: [5, 6, 7],
mass: 8,
population: 1000,
habitable: true
}
}
]
}
const bad = {
type: 'ship',
location: [1, 2, 'a'],
mass: 4,
name: 'foo',
crew: [
{
name: 'bar',
age: 44,
rank: 'captain',
home: {
type: 'planet',
location: [5, 6, 7],
mass: 8,
population: 'a',
habitable: true
}
}
]
}
// console.log(T.decode(good))
// console.log(T.decode(bad))
suite
.add('space-object (good)', function () {
T.decode(good)
})
.add('space-object (bad)', function () {
T.decode(bad)
})
.on('cycle', function (event: any) {
console.log(String(event.target))
})
.on('complete', function (this: any) {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })