-
Notifications
You must be signed in to change notification settings - Fork 0
/
heavyBenchmark.ts
109 lines (99 loc) · 2.06 KB
/
heavyBenchmark.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 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 test = {
deliveryName: 'tes',
products: Array.from({ length: 10000 }).map((_, index) => ({
productName: index
}))
};
type Test = {
deliveryName: string
products: Array<{
productName: string
}>
}
const schemaValidator: ValidationSchema<typeof test> = {
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<Test>({
deliveryName: joi.string().min(3),
products: joi.array().items(
joi.object({
productName: joi.string().min(3)
})
)
});
const schema = object<Test>({
deliveryName: string().min(3).required(),
products: array(
object({
productName: string().min(3).required()
}).required()
).required()
}).compile();
const suite = new Benchmark.Suite();
suite
.add('@resourge/schema', function () {
schema.validate(test as any);
})
.add('Fast Validator', function () {
check(test);
})
.add('joi', function () {
try {
joiSchema.validate(test, { abortEarly: false });
}
catch {}
})
.add('zod', function () {
try {
zodSchema.parse(test);
}
catch {}
})
.add('Yup', function () {
try {
yupSchema.validateSync(test, { 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 });