-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathnested_object.ts
More file actions
116 lines (108 loc) · 2.58 KB
/
nested_object.ts
File metadata and controls
116 lines (108 loc) · 2.58 KB
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'
function getData() {
return {
username: 'virk',
password: 'secret',
contact: {
name: 'virk',
address: 'universe',
},
}
}
const zodSchema = z.object({
username: z.string(),
password: z.string(),
contact: z.object({
name: z.string(),
address: z.string().optional(),
}),
})
const yupSchema = yup
.object({
username: yup.string().required(),
password: yup.string().required(),
contact: yup
.object({
name: yup.string().required(),
address: yup.string(),
})
.required(),
})
.required()
const vineSchema = vine.compile(
vine.object({
username: vine.string(),
password: vine.string(),
contact: vine.object({
name: vine.string(),
address: vine.string().optional(),
}),
})
)
const valibotSchame = valibot.object({
username: valibot.string(),
password: valibot.string(),
contact: valibot.object({
name: valibot.string(),
address: valibot.optional(valibot.string()),
}),
})
const joiSchema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
contact: Joi.object({
name: Joi.string().required(),
address: Joi.string(),
}).required(),
}).required()
console.log('=================================')
console.log('Benchmarking with nested 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(valibotSchame, getData()).then(() => deferred.resolve())
},
})
.add('Joi', {
defer: true,
fn: function (deferred: any) {
joiSchema
.validateAsync(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 })