forked from vinejs/vine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flat_object.ts
116 lines (107 loc) · 2.59 KB
/
flat_object.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
110
111
112
113
114
115
116
// @ts-ignore
import Benchmark from 'benchmark'
import { z } from 'zod'
import * as yup from 'yup'
import vine from '../index.js'
import * as valibot from 'valibot'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'
function getData() {
return {
username: 'virk',
password: 'secret',
}
}
const zodSchema = z.object({
username: z.string(),
password: z.string(),
})
const yupSchema = yup
.object({
username: yup.string().required(),
password: yup.string().required(),
})
.required()
const vineSchema = vine.compile(
vine.object({
username: vine.string(),
password: vine.string(),
})
)
const valibotSchema = valibot.object({
username: valibot.string(),
password: valibot.string(),
})
const joiSchema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
}).required()
const ajv = new Ajv.default()
interface AjvData {
username: string
password: string
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
username: { type: 'string', nullable: false },
password: { type: 'string', nullable: false },
},
required: ['username', 'password'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)
console.log('===============================')
console.log('Benchmarking with flat object')
console.log('===============================')
const suite = new Benchmark.Suite()
suite
.add('Vine', {
defer: true,
fn: function (deferred: any) {
vineSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Zod', {
defer: true,
fn: function (deferred: any) {
zodSchema.parseAsync(getData()).then(() => deferred.resolve())
},
})
.add('Yup', {
defer: true,
fn: function (deferred: any) {
yupSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Valibot', {
defer: true,
fn: function (deferred: any) {
valibot.parseAsync(valibotSchema, getData()).then(() => deferred.resolve())
},
})
.add('Joi', {
defer: true,
fn: function (deferred: any) {
joiSchema
.validateAsync(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
})
.add('Ajv', {
defer: true,
fn: function (deferred: any) {
ajvValidator(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
})
.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 })