-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import B from "benchmark"; | ||
import { z } from "zod"; | ||
import * as V from "valibot"; | ||
import * as S from "rescript-schema/src/S.mjs"; | ||
import * as v from "valibot"; | ||
import * as S from "rescript-schema/src/S.js"; | ||
import { type } from "arktype"; | ||
|
||
const data = Object.freeze({ | ||
|
@@ -33,6 +33,33 @@ const arkType = type({ | |
}, | ||
}); | ||
|
||
const RescriptSchemaUnion = S.union([ | ||
{ box: S.string }, | ||
S.coerce(S.string, S.number), | ||
]); | ||
// S.parseOrThrow("123", RescriptSchemaUnion) | ||
// [email protected] x 82,715,204 ops/sec ±2.11% (86 runs sampled) | ||
|
||
const ValibotUnion = v.union([ | ||
v.object({ | ||
box: v.string(), | ||
}), | ||
v.pipe(v.string(), v.decimal(), v.transform(Number)), | ||
]); | ||
// v.parse(ValibotUnion, "123") | ||
// [email protected] x 5,507,472 ops/sec ±0.38% (98 runs sampled) | ||
|
||
const ArkTypeUnion = type({ box: "string" }).or("string.numeric.parse"); | ||
// ArkTypeUnion("123") | ||
// [email protected] x 4,300,118 ops/sec ±0.33% (97 runs sampled) | ||
|
||
const ZodUnion = z.union([ | ||
z.object({ box: z.string() }), | ||
z.string().pipe(z.coerce.number()), | ||
]); | ||
// ZodUnion.parse("123") | ||
// [email protected] x 3,278,494 ops/sec ±0.55% (94 runs sampled) | ||
|
||
const zodSchema = z.object({ | ||
number: z.number(), | ||
negNumber: z.number(), | ||
|
@@ -47,17 +74,17 @@ const zodSchema = z.object({ | |
}), | ||
}); | ||
|
||
const valibotSchema = V.object({ | ||
number: V.number(), | ||
negNumber: V.number(), | ||
maxNumber: V.number(), | ||
string: V.string(), | ||
longString: V.string(), | ||
boolean: V.boolean(), | ||
deeplyNested: V.object({ | ||
foo: V.string(), | ||
num: V.number(), | ||
bool: V.boolean(), | ||
const valibotSchema = v.object({ | ||
number: v.number(), | ||
negNumber: v.number(), | ||
maxNumber: v.number(), | ||
string: v.string(), | ||
longString: v.string(), | ||
boolean: v.boolean(), | ||
deeplyNested: v.object({ | ||
foo: v.string(), | ||
num: v.number(), | ||
bool: v.boolean(), | ||
}), | ||
}); | ||
|
||
|
@@ -77,7 +104,7 @@ const schema = S.schema({ | |
bool: S.boolean, | ||
}, | ||
}); | ||
let parseOrThrow = S.compile(schema, "Input", "Output", "Sync", true); | ||
const parseOrThrow = S.compile(schema, "Input", "Output", "Sync", true); | ||
|
||
new B.Suite() | ||
.add("rescript-schema (create)", () => { | ||
|
@@ -117,6 +144,9 @@ new B.Suite() | |
}); | ||
return S.parseOrThrow(data, schema); | ||
}) | ||
.add("rescript-schema (union)", () => { | ||
return S.parseOrThrow("123", RescriptSchemaUnion); | ||
}) | ||
.add("Zod (create)", () => { | ||
return z.object({ | ||
number: z.number(), | ||
|
@@ -151,39 +181,45 @@ new B.Suite() | |
}); | ||
return zodSchema.parse(data); | ||
}) | ||
.add("Zod (union)", () => { | ||
return ZodUnion.parse("123"); | ||
}) | ||
.add("Valibot (create)", () => { | ||
return V.object({ | ||
number: V.number(), | ||
negNumber: V.number(), | ||
maxNumber: V.number(), | ||
string: V.string(), | ||
longString: V.string(), | ||
boolean: V.boolean(), | ||
deeplyNested: V.object({ | ||
foo: V.string(), | ||
num: V.number(), | ||
bool: V.boolean(), | ||
return v.object({ | ||
number: v.number(), | ||
negNumber: v.number(), | ||
maxNumber: v.number(), | ||
string: v.string(), | ||
longString: v.string(), | ||
boolean: v.boolean(), | ||
deeplyNested: v.object({ | ||
foo: v.string(), | ||
num: v.number(), | ||
bool: v.boolean(), | ||
}), | ||
}); | ||
}) | ||
.add("Valibot (parse)", () => { | ||
return V.parse(valibotSchema, data); | ||
return v.parse(valibotSchema, data); | ||
}) | ||
.add("Valibot (create + parse)", () => { | ||
const valibotSchema = V.object({ | ||
number: V.number(), | ||
negNumber: V.number(), | ||
maxNumber: V.number(), | ||
string: V.string(), | ||
longString: V.string(), | ||
boolean: V.boolean(), | ||
deeplyNested: V.object({ | ||
foo: V.string(), | ||
num: V.number(), | ||
bool: V.boolean(), | ||
const valibotSchema = v.object({ | ||
number: v.number(), | ||
negNumber: v.number(), | ||
maxNumber: v.number(), | ||
string: v.string(), | ||
longString: v.string(), | ||
boolean: v.boolean(), | ||
deeplyNested: v.object({ | ||
foo: v.string(), | ||
num: v.number(), | ||
bool: v.boolean(), | ||
}), | ||
}); | ||
return V.parse(valibotSchema, data); | ||
return v.parse(valibotSchema, data); | ||
}) | ||
.add("Valibot (union)", () => { | ||
return v.parse(ValibotUnion, "123"); | ||
}) | ||
.add("ArkType (create)", () => { | ||
return type({ | ||
|
@@ -219,6 +255,9 @@ new B.Suite() | |
}); | ||
return arkType(data); | ||
}) | ||
.add("ArkType (union)", () => { | ||
return ArkTypeUnion("123"); | ||
}) | ||
.on("cycle", (event) => { | ||
console.log(String(event.target)); | ||
}) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
e234b0e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
S.schema - make
1390389
ops/sec (±0.61%
)1333035
ops/sec (±0.94%
)0.96
S.schema - make + parse
107382
ops/sec (±1.29%
)107810
ops/sec (±1.18%
)1.00
S.schema - parse
49787032
ops/sec (±1.37%
)49514611
ops/sec (±1.45%
)0.99
S.schema - parse strict
21589492
ops/sec (±1.60%
)23185793
ops/sec (±1.21%
)1.07
S.schema - make + reverse
945116
ops/sec (±0.32%
)916551
ops/sec (±0.35%
)0.97
S.schema - make + reverse convert
190356
ops/sec (±0.69%
)178335
ops/sec (±1.39%
)0.94
S.schema - reverse convert
68213474
ops/sec (±1.84%
)56582336
ops/sec (±1.79%
)0.83
S.schema - reverse convert (compiled)
139811590
ops/sec (±4.05%
)136263821
ops/sec (±4.28%
)0.97
S.schema - assert
64597085
ops/sec (±1.94%
)51172085
ops/sec (±2.32%
)0.79
S.schema - assert (compiled)
73662240
ops/sec (±2.05%
)72465443
ops/sec (±2.31%
)0.98
S.schema - assert strict
23253733
ops/sec (±1.12%
)21906524
ops/sec (±0.73%
)0.94
S.object - make
962117
ops/sec (±0.30%
)965712
ops/sec (±0.36%
)1.00
S.object - make + parse
88921
ops/sec (±0.39%
)89541
ops/sec (±0.22%
)1.01
S.object - parse
36712677
ops/sec (±1.53%
)33576635
ops/sec (±1.02%
)0.91
S.object - make + reverse
131330
ops/sec (±1.23%
)128955
ops/sec (±1.24%
)0.98
S.object - make + reverse convert
85512
ops/sec (±0.86%
)83466
ops/sec (±1.10%
)0.98
S.object - reverse convert
47757735
ops/sec (±1.40%
)46593380
ops/sec (±1.70%
)0.98
S.string - parse
69178075
ops/sec (±2.37%
)63773163
ops/sec (±2.49%
)0.92
S.string - reverse convert
72029079
ops/sec (±1.76%
)67690417
ops/sec (±2.11%
)0.94
This comment was automatically generated by workflow using github-action-benchmark.