Revision 0.8.1

This commit is contained in:
sinclair
2024-12-25 15:00:11 +09:00
parent 6d32479372
commit c4ff05bd55
5 changed files with 42 additions and 43 deletions

View File

@@ -9,7 +9,7 @@ import * as z from 'zod'
// Benchmark
// ------------------------------------------------------------------
function benchmark(library: string, using: string, callback: Function) {
const [now, iterations] = [Date.now(), 1_000_000]
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 }
@@ -19,66 +19,66 @@ function benchmark(library: string, using: string, callback: Function) {
// ------------------------------------------------------------------
function zod() {
const T = z.object({
x: z.number(),
x: z.string(),
y: z.number(),
z: z.number(),
z: z.boolean(),
})
return benchmark('zod', 'zod', () => T.safeParse({ x: 1, y: 2, z: 3 }).success)
return benchmark('zod', 'zod', () => T.safeParse({ x: 'hello', y: 42, z: true }).success)
}
function zod_using_value() {
const T = Box(
z.object({
x: z.number(),
x: z.string(),
y: z.number(),
z: z.number(),
z: z.boolean(),
}),
)
return benchmark('zod', 'typebox:value', () => Value.Check(T, { x: 1, y: 2, z: 3 }))
return benchmark('zod', 'typebox:value', () => Value.Check(T, { x: 'hello', y: 42, z: true }))
}
function zod_using_compiler() {
const T = TypeCompiler.Compile(
Box(
z.object({
x: z.number(),
x: z.string(),
y: z.number(),
z: z.number(),
z: z.boolean(),
}),
),
)
return benchmark('zod', 'typebox:compile', () => T.Check({ x: 1, y: 2, z: 3 }))
return benchmark('zod', 'typebox:compile', () => T.Check({ x: 'hello', y: 42, z: true }))
}
// ------------------------------------------------------------------
// Valibot
// ------------------------------------------------------------------
function valibot() {
const T = v.object({
x: v.number(),
x: v.string(),
y: v.number(),
z: v.number(),
z: v.boolean(),
})
return benchmark('valibot', 'valibot', () => v.safeParse(T, { x: 1, y: 2, z: 3 }).success)
return benchmark('valibot', 'valibot', () => v.safeParse(T, { x: 'hello', y: 42, z: true }).success)
}
function valibot_using_value() {
const T = Box(
v.object({
x: v.number(),
x: v.string(),
y: v.number(),
z: v.number(),
z: v.boolean(),
}),
)
return benchmark('valibot', 'typebox:value', () => Value.Check(T, { x: 1, y: 2, z: 3 }))
return benchmark('valibot', 'typebox:value', () => Value.Check(T, { x: 'hello', y: 42, z: true }))
}
function valibot_using_compiler() {
const T = TypeCompiler.Compile(
Box(
v.object({
x: v.number(),
x: v.string(),
y: v.number(),
z: v.number(),
z: v.boolean(),
}),
),
)
return benchmark('valibot', 'typebox:compile', () => T.Check({ x: 1, y: 2, z: 3 }))
return benchmark('valibot', 'typebox:compile', () => T.Check({ x: 'hello', y: 42, z: true }))
}
console.table([valibot(), valibot_using_value(), valibot_using_compiler()])