Skip to content

Commit

Permalink
chore(benchmarks): add valibot
Browse files Browse the repository at this point in the history
  • Loading branch information
nakrovati committed Feb 14, 2024
1 parent d6f5589 commit 02ff0a5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions benchmarks/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import * as yup from 'yup'
import vine from '../index.js'
import * as valibot from 'valibot'

function getData() {
return {
Expand Down Expand Up @@ -54,6 +55,15 @@ const vineSchema = vine.compile(
})
)

const valibotSchema = valibot.object({
contacts: valibot.array(
valibot.object({
type: valibot.string(),
value: valibot.string(),
})
),
})

console.log('======================')
console.log('Benchmarking arrays')
console.log('======================')
Expand All @@ -78,6 +88,12 @@ suite
yupSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Valibot', {
defer: true,
fn: function (deferred: any) {
valibot.parseAsync(valibotSchema, getData()).then(() => deferred.resolve())
},
})
.on('cycle', function (event: any) {
console.log(String(event.target))
})
Expand Down
12 changes: 12 additions & 0 deletions benchmarks/flat_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import * as yup from 'yup'
import vine from '../index.js'
import * as valibot from 'valibot'

function getData() {
return {
Expand Down Expand Up @@ -30,6 +31,11 @@ const vineSchema = vine.compile(
})
)

const valibotSchema = valibot.object({
username: valibot.string(),
password: valibot.string(),
})

console.log('===============================')
console.log('Benchmarking with flat object')
console.log('===============================')
Expand All @@ -54,6 +60,12 @@ suite
yupSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Valibot', {
defer: true,
fn: function (deferred: any) {
valibot.parseAsync(valibotSchema, getData()).then(() => deferred.resolve())
},
})
.on('cycle', function (event: any) {
console.log(String(event.target))
})
Expand Down
16 changes: 16 additions & 0 deletions benchmarks/nested_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import * as yup from 'yup'
import vine from '../index.js'
import * as valibot from 'valibot'

function getData() {
return {
Expand Down Expand Up @@ -48,6 +49,15 @@ const vineSchema = vine.compile(
})
)

const valibotSchame = valibot.object({
username: valibot.string(),
password: valibot.string(),
contact: valibot.object({
name: valibot.string(),
address: valibot.optional(valibot.string()),
}),
})

console.log('=================================')
console.log('Benchmarking with nested object')
console.log('=================================')
Expand All @@ -72,6 +82,12 @@ suite
yupSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Valibot', {
defer: true,
fn: function (deferred: any) {
valibot.parseAsync(valibotSchame, getData()).then(() => deferred.resolve())
},
})
.on('cycle', function (event: any) {
console.log(String(event.target))
})
Expand Down
20 changes: 20 additions & 0 deletions benchmarks/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Benchmark from 'benchmark'
import { z } from 'zod'
import vine from '../index.js'
import * as valibot from 'valibot'

function getData() {
return {
Expand Down Expand Up @@ -46,6 +47,19 @@ const vineSchema = vine.compile(
})
)

const valibotSchema = valibot.object({
contact: valibot.union([
valibot.object({
type: valibot.literal('email'),
email: valibot.string(),
}),
valibot.object({
type: valibot.literal('phone'),
mobile_number: valibot.string(),
}),
]),
})

console.log('=======================')
console.log('Benchmarking unions')
console.log('=======================')
Expand All @@ -64,6 +78,12 @@ suite
zodSchema.parseAsync(getData()).then(() => deferred.resolve())
},
})
.add('Valibot', {
defer: true,
fn: function (deferred: any) {
valibot.parseAsync(valibotSchema, getData()).then(() => deferred.resolve())
},
})
.on('cycle', function (event: any) {
console.log(String(event.target))
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"ts-node": "^10.9.2",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"valibot": "^0.28.1",
"yup": "^1.3.3",
"zod": "^3.22.4"
},
Expand Down

0 comments on commit 02ff0a5

Please sign in to comment.