From 02ff0a5b6a6a50e672d7277b4a0c99bd3fe1b2e5 Mon Sep 17 00:00:00 2001 From: Daniil Zhuravlev Date: Wed, 14 Feb 2024 13:28:04 +0400 Subject: [PATCH] chore(benchmarks): add valibot --- benchmarks/array.ts | 16 ++++++++++++++++ benchmarks/flat_object.ts | 12 ++++++++++++ benchmarks/nested_object.ts | 16 ++++++++++++++++ benchmarks/union.ts | 20 ++++++++++++++++++++ package.json | 1 + 5 files changed, 65 insertions(+) diff --git a/benchmarks/array.ts b/benchmarks/array.ts index 6953ad3..3cfc101 100644 --- a/benchmarks/array.ts +++ b/benchmarks/array.ts @@ -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 { @@ -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('======================') @@ -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)) }) diff --git a/benchmarks/flat_object.ts b/benchmarks/flat_object.ts index 6b46c97..d623abc 100644 --- a/benchmarks/flat_object.ts +++ b/benchmarks/flat_object.ts @@ -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 { @@ -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('===============================') @@ -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)) }) diff --git a/benchmarks/nested_object.ts b/benchmarks/nested_object.ts index 38e20c6..1094dae 100644 --- a/benchmarks/nested_object.ts +++ b/benchmarks/nested_object.ts @@ -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 { @@ -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('=================================') @@ -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)) }) diff --git a/benchmarks/union.ts b/benchmarks/union.ts index 42d5dcf..2de5938 100644 --- a/benchmarks/union.ts +++ b/benchmarks/union.ts @@ -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 { @@ -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('=======================') @@ -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)) }) diff --git a/package.json b/package.json index 595807b..7937966 100644 --- a/package.json +++ b/package.json @@ -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" },