|
| 1 | +import { z } from 'zod' |
| 2 | +// @ts-ignore |
| 3 | +import Benchmark from 'benchmark' |
| 4 | +import Ajv, { AsyncValidateFunction } from 'ajv' |
| 5 | +import { Compiler } from '../src/compiler/main.js' |
| 6 | +import { ErrorReporterFactory } from '../factories/error_reporter.js' |
| 7 | + |
| 8 | +const suite = new Benchmark.Suite() |
| 9 | + |
| 10 | +const data = { contacts: [{ email: 'foo@bar.com' }] } |
| 11 | +const meta = {} |
| 12 | +const refs = { |
| 13 | + 'ref://1': { |
| 14 | + validator(value: unknown, _: any, ctx: any) { |
| 15 | + if (typeof value !== 'string') { |
| 16 | + ctx.report('Value is not a string', ctx) |
| 17 | + } |
| 18 | + }, |
| 19 | + options: {}, |
| 20 | + }, |
| 21 | +} |
| 22 | +const errorReporter = new ErrorReporterFactory().create() |
| 23 | + |
| 24 | +const zodSchema = z.object({ |
| 25 | + contacts: z.array( |
| 26 | + z.object({ |
| 27 | + email: z.string(), |
| 28 | + }) |
| 29 | + ), |
| 30 | +}) |
| 31 | + |
| 32 | +const compiler = new Compiler({ |
| 33 | + type: 'root', |
| 34 | + schema: { |
| 35 | + type: 'object', |
| 36 | + allowNull: false, |
| 37 | + isOptional: false, |
| 38 | + fieldName: '*', |
| 39 | + allowUnknownProperties: false, |
| 40 | + bail: true, |
| 41 | + properties: [ |
| 42 | + { |
| 43 | + type: 'array', |
| 44 | + fieldName: 'contacts', |
| 45 | + propertyName: 'contacts', |
| 46 | + allowNull: false, |
| 47 | + bail: true, |
| 48 | + isOptional: false, |
| 49 | + each: { |
| 50 | + type: 'object', |
| 51 | + propertyName: '*', |
| 52 | + allowNull: false, |
| 53 | + allowUnknownProperties: false, |
| 54 | + isOptional: false, |
| 55 | + fieldName: '*', |
| 56 | + bail: true, |
| 57 | + groups: [], |
| 58 | + properties: [ |
| 59 | + { |
| 60 | + type: 'literal', |
| 61 | + bail: true, |
| 62 | + allowNull: false, |
| 63 | + fieldName: 'email', |
| 64 | + propertyName: 'email', |
| 65 | + isOptional: false, |
| 66 | + validations: [ |
| 67 | + { |
| 68 | + isAsync: false, |
| 69 | + ruleFnId: 'ref://1', |
| 70 | + implicit: false, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ], |
| 75 | + validations: [], |
| 76 | + }, |
| 77 | + validations: [], |
| 78 | + }, |
| 79 | + ], |
| 80 | + groups: [], |
| 81 | + propertyName: '*', |
| 82 | + validations: [], |
| 83 | + }, |
| 84 | +}) |
| 85 | + |
| 86 | +compiler.compile() |
| 87 | +const fn = compiler.compile() |
| 88 | + |
| 89 | +const ajv = new Ajv.default({ removeAdditional: true }) |
| 90 | +const ajvSchema = { |
| 91 | + $async: true, |
| 92 | + type: 'object', |
| 93 | + properties: { |
| 94 | + contacts: { |
| 95 | + type: 'array', |
| 96 | + items: { |
| 97 | + type: 'object', |
| 98 | + properties: { |
| 99 | + email: { type: 'string' }, |
| 100 | + }, |
| 101 | + required: ['email'], |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + required: ['contacts'], |
| 106 | +} |
| 107 | + |
| 108 | +const ajvValidate = ajv.compile(ajvSchema) as AsyncValidateFunction<any> |
| 109 | + |
| 110 | +suite |
| 111 | + .add('VineJS compiler', { |
| 112 | + defer: true, |
| 113 | + |
| 114 | + // benchmark test function |
| 115 | + fn: function (deferred: any) { |
| 116 | + fn(data, meta, refs, errorReporter).then(() => deferred.resolve()) |
| 117 | + }, |
| 118 | + }) |
| 119 | + .add('Ajv', { |
| 120 | + defer: true, |
| 121 | + |
| 122 | + // benchmark test function |
| 123 | + fn: function (deferred: any) { |
| 124 | + ajvValidate(data).then(() => deferred.resolve()) |
| 125 | + }, |
| 126 | + }) |
| 127 | + .add('Zod', { |
| 128 | + defer: true, |
| 129 | + fn(deferred: any) { |
| 130 | + zodSchema.parseAsync(data).then(() => deferred.resolve()) |
| 131 | + }, |
| 132 | + }) |
| 133 | + .on('cycle', function (event: any) { |
| 134 | + console.log(String(event.target)) |
| 135 | + }) |
| 136 | + .on('complete', function (this: any) { |
| 137 | + console.log('Fastest is ' + this.filter('fastest').map('name')) |
| 138 | + }) |
| 139 | + .run({ async: true }) |
0 commit comments