Skip to content

Commit 38a9871

Browse files
committed
Add dhi - 1.78x faster than Zod (average)
Performance results: - parseSafe: 7.02M ops/s (3.37x faster than Zod) - parseStrict: 1.46M ops/s (1.11x faster than Zod) - assertLoose: 1.87M ops/s (1.47x faster than Zod) - assertStrict: 1.42M ops/s (1.16x faster than Zod) dhi is a WASM-powered validation library with: - Full Zod API compatibility - 9.2KB bundle size - Zero dependencies - Works in Node, Bun, Deno, and browsers Links: - npm: https://www.npmjs.com/package/dhi - GitHub: https://github.com/justrach/satya-zig - Docs: https://github.com/justrach/satya-zig/blob/main/js-bindings/README.md
1 parent 3789fe6 commit 38a9871

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

cases/dhi.ts

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { z } from 'dhi/schema';
22
import { createCase } from '../benchmarks';
33

4-
// Note: dhi doesn't support .strict() or .passthrough() yet
5-
// All cases use the same validation (which is the default behavior)
6-
74
createCase('dhi', 'parseSafe', () => {
85
const dataType = z.object({
96
number: z.number(),
@@ -17,47 +14,55 @@ createCase('dhi', 'parseSafe', () => {
1714
num: z.number(),
1815
bool: z.boolean(),
1916
}),
20-
});
17+
}); // Default behavior: strips unknown keys
2118

2219
return data => {
2320
return dataType.parse(data);
2421
};
2522
});
2623

2724
createCase('dhi', 'parseStrict', () => {
28-
const dataType = z.object({
29-
number: z.number(),
30-
negNumber: z.number(),
31-
maxNumber: z.number(),
32-
string: z.string(),
33-
longString: z.string(),
34-
boolean: z.boolean(),
35-
deeplyNested: z.object({
36-
foo: z.string(),
37-
num: z.number(),
38-
bool: z.boolean(),
39-
}),
40-
});
25+
const dataType = z
26+
.object({
27+
number: z.number(),
28+
negNumber: z.number(),
29+
maxNumber: z.number(),
30+
string: z.string(),
31+
longString: z.string(),
32+
boolean: z.boolean(),
33+
deeplyNested: z
34+
.object({
35+
foo: z.string(),
36+
num: z.number(),
37+
bool: z.boolean(),
38+
})
39+
.strict(), // Throw on unknown keys (nested)
40+
})
41+
.strict(); // Throw on unknown keys (root)
4142

4243
return data => {
4344
return dataType.parse(data);
4445
};
4546
});
4647

4748
createCase('dhi', 'assertLoose', () => {
48-
const dataType = z.object({
49-
number: z.number(),
50-
negNumber: z.number(),
51-
maxNumber: z.number(),
52-
string: z.string(),
53-
longString: z.string(),
54-
boolean: z.boolean(),
55-
deeplyNested: z.object({
56-
foo: z.string(),
57-
num: z.number(),
58-
bool: z.boolean(),
59-
}),
60-
});
49+
const dataType = z
50+
.object({
51+
number: z.number(),
52+
negNumber: z.number(),
53+
maxNumber: z.number(),
54+
string: z.string(),
55+
longString: z.string(),
56+
boolean: z.boolean(),
57+
deeplyNested: z
58+
.object({
59+
foo: z.string(),
60+
num: z.number(),
61+
bool: z.boolean(),
62+
})
63+
.passthrough(), // Allow unknown keys (nested)
64+
})
65+
.passthrough(); // Allow unknown keys (root)
6166

6267
return data => {
6368
dataType.parse(data);
@@ -66,19 +71,23 @@ createCase('dhi', 'assertLoose', () => {
6671
});
6772

6873
createCase('dhi', 'assertStrict', () => {
69-
const dataType = z.object({
70-
number: z.number(),
71-
negNumber: z.number(),
72-
maxNumber: z.number(),
73-
string: z.string(),
74-
longString: z.string(),
75-
boolean: z.boolean(),
76-
deeplyNested: z.object({
77-
foo: z.string(),
78-
num: z.number(),
79-
bool: z.boolean(),
80-
}),
81-
});
74+
const dataType = z
75+
.object({
76+
number: z.number(),
77+
negNumber: z.number(),
78+
maxNumber: z.number(),
79+
string: z.string(),
80+
longString: z.string(),
81+
boolean: z.boolean(),
82+
deeplyNested: z
83+
.object({
84+
foo: z.string(),
85+
num: z.number(),
86+
bool: z.boolean(),
87+
})
88+
.strict(), // Throw on unknown keys (nested)
89+
})
90+
.strict(); // Throw on unknown keys (root)
8291

8392
return data => {
8493
dataType.parse(data);

0 commit comments

Comments
 (0)