/
benchmark.ts
103 lines (95 loc) · 2 KB
/
benchmark.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
import Benchmark from 'benchmark';
import Validator, { ValidationSchema } from 'fastest-validator';
import { object, array, string } from '../src/lib/index';
import * as yup from 'yup';
import { z } from 'zod';
import joi from 'joi';
const v = new Validator();
const delivery = {
deliveryName: 'te',
products: [
{
productName: 'testObject 1'
}
]
};
const schemaValidator: ValidationSchema<typeof delivery> = {
deliveryName: { type: 'string', min: 3 },
products: {
type: 'array',
items: {
type: "object",
props: {
productName: { type: 'string', min: 3 }
}
}
}
};
const check = v.compile(schemaValidator);
const yupSchema = yup.object().shape({
deliveryName: yup.string().min(3),
products: yup.array().of(
yup.object().shape({
productName: yup.string().min(3)
})
)
});
const zodSchema = z.object({
deliveryName: z.string().min(3),
products: z.array(
z.object({
productName: z.string().min(3)
})
)
});
const joiSchema = joi.object<typeof delivery>({
deliveryName: joi.string().min(3),
products: joi.array().items(
joi.object({
productName: joi.string().min(3)
})
)
});
const schema = object<typeof delivery>({
deliveryName: string().required().min(3),
products: array(
object({
productName: string().required().min(3)
}).required()
).required()
}).compile();
const suite = new Benchmark.Suite();
suite
.add('@resourge/schema', function () {
schema.validate(delivery);
})
.add('Fast Validator', function () {
check(delivery);
})
.add('joi', function () {
try {
joiSchema.validate(delivery, { abortEarly: false });
}
catch {}
})
.add('zod', function () {
try {
zodSchema.parse(delivery);
}
catch {}
})
.add('Yup', function () {
try {
yupSchema.validateSync(delivery, { abortEarly: false });
}
catch {}
})
.on('cycle', function (event: any) {
console.log(String(event.target));
})
.on('complete', function () {
// @ts-expect-error
console.log('Fastest is ', this.filter('fastest').map('name'));
})
// run async
.run({ async: false, minSamples: 1000 });