Files
typemap/task/benchmark/index.ts
sinclairzx81 5ce19b2f4f Revision 0.8.0 (#9)
- Rename to TypeMap
2025-01-26 03:27:38 +09:00

83 lines
2.5 KiB
TypeScript

import { Value } from '@sinclair/typebox/value'
import { Compile, TypeBox } from '@sinclair/typemap'
import * as v from 'valibot'
import * as z from 'zod'
// ------------------------------------------------------------------
// Benchmark
// ------------------------------------------------------------------
function benchmark(library: string, using: string, callback: Function) {
const [now, iterations] = [Date.now(), 10_000_000]
for (let i = 0; i < iterations; i++) if (!callback()) throw Error('Invalid' + library + using)
const elapsed = `${Date.now() - now} ms`.padEnd(8)
return { library: library.padEnd(12), using: using.padEnd(16), iterations, elapsed }
}
// ------------------------------------------------------------------
// Zod
// ------------------------------------------------------------------
function zod() {
const T = z.object({
x: z.string(),
y: z.number(),
z: z.boolean(),
})
return benchmark('zod', 'zod', () => T.safeParse({ x: 'hello', y: 42, z: true }).success)
}
function zod_using_value() {
const T = TypeBox(
z.object({
x: z.string(),
y: z.number(),
z: z.boolean(),
}),
)
return benchmark('zod', 'typebox:value', () => Value.Check(T, { x: 'hello', y: 42, z: true }))
}
function zod_using_compiler() {
const T = Compile(
z.object({
x: z.string(),
y: z.number(),
z: z.boolean(),
}),
)
return benchmark('zod', 'typebox:compile', () => T.Check({ x: 'hello', y: 42, z: true }))
}
// ------------------------------------------------------------------
// Valibot
// ------------------------------------------------------------------
function valibot() {
const T = v.object({
x: v.string(),
y: v.number(),
z: v.boolean(),
})
return benchmark('valibot', 'valibot', () => v.safeParse(T, { x: 'hello', y: 42, z: true }).success)
}
function valibot_using_value() {
const T = TypeBox(
v.object({
x: v.string(),
y: v.number(),
z: v.boolean(),
}),
)
return benchmark('valibot', 'typebox:value', () => Value.Check(T, { x: 'hello', y: 42, z: true }))
}
function valibot_using_compiler() {
const T = Compile(
v.object({
x: v.string(),
y: v.number(),
z: v.boolean(),
}),
)
return benchmark('valibot', 'typebox:compile', () => T.Check({ x: 'hello', y: 42, z: true }))
}
console.log('running benchmark')
console.table([valibot(), valibot_using_value(), valibot_using_compiler()])
console.table([zod(), zod_using_value(), zod_using_compiler()])