Skip to content

Commit

Permalink
chore(benchmarks): add ajv and joi benchmark libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
lolo32 committed Jun 22, 2023
1 parent 92697c4 commit ffb2a4e
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
55 changes: 55 additions & 0 deletions benchmarks/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import yup from 'yup'
import vine from '../index.js'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'

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

const joiSchema = Joi.object({
contacts: Joi.array()
.items(
Joi.object({
type: Joi.string().required(),
value: Joi.string().required(),
})
)
.required(),
}).required()

const ajv = new Ajv.default()
interface AjvData {
contacts: [{ type: string; value: string }]
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
contacts: {
type: 'array',
items: {
type: 'object',
properties: {
type: { type: 'string', nullable: false },
value: { type: 'string', nullable: false },
},
required: ['type', 'value'],
},
},
},
required: ['contacts'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)

console.log('======================')
console.log('Benchmarking arrays')
console.log('======================')
Expand Down Expand Up @@ -87,6 +125,23 @@ suite
.catch(console.log)
},
})
.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))
})
Expand Down
41 changes: 41 additions & 0 deletions benchmarks/flat_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import yup from 'yup'
import vine from '../index.js'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'

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

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('===============================')
Expand Down Expand Up @@ -63,6 +87,23 @@ suite
.catch(console.log)
},
})
.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))
})
Expand Down
57 changes: 57 additions & 0 deletions benchmarks/nested_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import yup from 'yup'
import vine from '../index.js'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'

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

const joiSchema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
contact: Joi.object({
name: Joi.string().required(),
address: Joi.string(),
}).required(),
}).required()

const ajv = new Ajv.default()
interface AjvData {
username: string
password: string
contact: {
name: string
address?: string
}
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
username: { type: 'string', nullable: false },
password: { type: 'string', nullable: false },
contact: {
type: 'object',
properties: {
name: { type: 'string', nullable: false },
address: { type: 'string' },
},
required: ['name'],
},
},
required: ['username', 'password', 'contact'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)

console.log('=================================')
console.log('Benchmarking with nested object')
console.log('=================================')
Expand Down Expand Up @@ -81,6 +121,23 @@ suite
.catch(console.log)
},
})
.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))
})
Expand Down
71 changes: 71 additions & 0 deletions benchmarks/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import Benchmark from 'benchmark'
import { z } from 'zod'
import vine from '../index.js'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'

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

const joiSchema = Joi.object({
contact: Joi.alternatives()
.try(
Joi.object({ type: 'email', email: Joi.string().required() }),
Joi.object({ type: 'phone', mobile_number: Joi.string().required() })
)
.required(),
}).required()

const ajv = new Ajv.default({ discriminator: true })
interface AjvEmail {
type: 'email'
email: string
}
interface AjvPhone {
type: 'phone'
mobile_number: string
}
interface AjvData {
contact: AjvEmail | AjvPhone
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
contact: {
type: 'object',
discriminator: { propertyName: 'type' },
required: ['type'],
oneOf: [
{
properties: {
type: { const: 'email' },
email: { type: 'string', nullable: false },
},
required: ['email'],
},
{
properties: {
type: { const: 'phone' },
mobile_number: { type: 'string', nullable: false },
},
required: ['mobile_number'],
},
],
},
},
required: ['contact'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)

console.log('=======================')
console.log('Benchmarking unions')
console.log('=======================')
Expand All @@ -70,6 +124,23 @@ suite
.catch(console.log)
},
})
.add('Joi', {
defer: true,
fn: function (deferred: any) {
joiSchema
.validateAsync(getData())
.then(() => deferred.resolve())
.catch((err) => console.dir(err, { depth: 20, colors: true }))
},
})
.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))
})
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@japa/spec-reporter": "^1.3.3",
"@swc/core": "^1.3.63",
"@types/node": "^20.3.1",
"ajv": "^8.12.0",
"benchmark": "^2.1.4",
"c8": "^8.0.0",
"del-cli": "^5.0.0",
Expand All @@ -61,6 +62,7 @@
"eslint-plugin-prettier": "^4.2.1",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"joi": "^17.9.2",
"np": "^8.0.4",
"prettier": "^2.8.7",
"ts-node": "^10.9.1",
Expand Down

0 comments on commit ffb2a4e

Please sign in to comment.