Skip to content

Commit

Permalink
Merge pull request #33 from nakrovati/develop
Browse files Browse the repository at this point in the history
Add Valibot to benchmarks
  • Loading branch information
RomainLanz authored Feb 14, 2024
2 parents b4c52f1 + 02ff0a5 commit f94d274
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
18 changes: 17 additions & 1 deletion benchmarks/array.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @ts-ignore
import Benchmark from 'benchmark'
import { z } from 'zod'
import yup from 'yup'
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
14 changes: 13 additions & 1 deletion benchmarks/flat_object.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @ts-ignore
import Benchmark from 'benchmark'
import { z } from 'zod'
import yup from 'yup'
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
18 changes: 17 additions & 1 deletion benchmarks/nested_object.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @ts-ignore
import Benchmark from 'benchmark'
import { z } from 'zod'
import yup from 'yup'
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 f94d274

Please sign in to comment.