Skip to content

Commit fe12d1b

Browse files
Shuunenjordan-boyer
authored andcommitted
chore: update benchmarking scripts and README for clarity and consistency
1 parent a66e0e6 commit fe12d1b

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ Here are the library used in this comparison :
3434
- [zod](https://github.com/colinhacks/zod) from Colin McDonnell
3535
- [zod/mini](https://github.com/colinhacks/zod) from Colin McDonnell
3636

37-
| Date | Score | Library | Deps | Size | Light | Input | Throw | Safe | Script exec | Lib exec | Fast | Readability |
38-
| ---------- | :---: | :-------: | :---: | :----: | :---: | :---: | :---: | :---: | :----------: | :---------: | :---: | :---------: |
39-
| 2025-04-13 | 0 | arktype | 2 | 147 KB | _0_ | **1** | _0_ | **1** | 873 ms _-1_ | 691 ms _-1_ | _0_ | **1** |
40-
| 2025-04-13 | 7 | valibot | **0** | 5 KB | **1** | **1** | **1** | **1** | 68 ms **1** | 11 ms **1** | **1** | _0_ |
41-
| 2025-04-13 | 7 | zod v3 | **0** | 60 KB | _0_ | **1** | **1** | **1** | 101 ms **1** | 35 ms **1** | **1** | **1** |
42-
| 2025-04-13 | 4 | zod v4 | 1 | 49 KB | _0_ | **1** | **1** | **1** | 222 ms _0_ | 144 ms _0_ | _0_ | **1** |
43-
| 2025-04-13 | 4 | @zod/mini | 1 | 21 KB | **1** | **1** | **1** | **1** | 243 ms _0_ | 168 ms _0_ | _0_ | **1** |
37+
| Date | Score | Library | Deps | Size | Light | Input A | Input B | Throw | Safe | Script exec | Lib exec | Fast | Readability |
38+
| ---------- | :---: | :-------: | :---: | :----: | :---: | :-----: | :-----: | :---: | :---: | :----------: | :---------: | :---: | :---------: |
39+
| 2025-04-13 | 2 | arktype | 2 | 147 KB | _0_ | **1** | **1** | _0_ | **1** | 875 ms _-1_ | 709 ms _-1_ | _0_ | **1** |
40+
| 2025-04-13 | 8 | valibot | **0** | 5 KB | **1** | **1** | **1** | **1** | **1** | 72 ms **1** | 10 ms **1** | **1** | _0_ |
41+
| 2025-04-13 | 4 | @zod/mini | 1 | 21 KB | **1** | **1** | _0_ | **1** | **1** | 218 ms _0_ | 131 ms _0_ | _0_ | _0_ |
42+
| 2025-04-13 | 8 | zod v3 | **0** | 60 KB | _0_ | **1** | **1** | **1** | **1** | 102 ms **1** | 35 ms **1** | **1** | **1** |
43+
| 2025-04-13 | 5 | zod v4 | 1 | 49 KB | _0_ | **1** | **1** | **1** | **1** | 225 ms _0_ | 144 ms _0_ | _0_ | **1** |
4444

4545
Legend :
4646

4747
- Deps : the number of dependencies of the library
4848
- Size : the minified build size in bytes of the related file in `src`, run `bun run build` to see by yourself
4949
- Light : 1 point if the build is `<= 20 KB`
50-
- Input : 1 point if the library can see that `age` is optional in the input but not optional in `type User` the output type
50+
- Input A : 1 point if the library can see that `age` is `optional, number` in the input but `not optional, number` in `type User` the output type
51+
- Input B : 1 point if the library can see that `phone` is `optional, string or number` in the input but `not optional, string` in `type User` the output type
5152
- Throw : 1 point if the library have a parse or throw method, useful when we don't want to handle the error cases
5253
- Safe : 1 point if the library have a safe parse method that will not throw and usually return a `Result` type
5354
- Script exec : 1 point if the average time in ms to execute the test file with bun is `<= 100ms`, check the `bun run bench` command output
@@ -69,7 +70,10 @@ Ok the build size is bigger but it does not impact the performance that much.
6970

7071
The 35ms vs 11ms seems like a 3x difference but it's the time to run 1000 iterations. So in the end it's not that much of a difference in a real world scenario.
7172

72-
For some weird reason zod4 seems to be slower than zod3
73+
## Remarks
74+
75+
1. For some weird reason Zod v4 seems to be slower than Zod v3
76+
2. Zod mini not handling the `or` operator is a blocker to me, hopefully it will be added in the future
7377

7478
## Todo
7579

@@ -100,7 +104,7 @@ Just open an issue or a PR, I'll be happy to discuss it with you <3
100104
- [Shields.io](https://shields.io) : for the nice badges on top of this readme
101105
- [Valibot](https://github.com/fabian-hiller/valibot) : for making this comparison possible
102106
- [Zod](https://github.com/colinhacks/zod) : for making this comparison possible
103-
107+
104108
## Stargazers over time
105109

106110
[![Stargazers over time](https://starchart.cc/Shuunen/validation-lib-comparison.svg?variant=adaptive)](https://starchart.cc/Shuunen/validation-lib-comparison)

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
},
4646
"scripts": {
4747
"bench": "bun bench:bun --warmup 5 --runs 10",
48-
"bench:bun": "hyperfine 'bun src/arktype.ts' 'bun src/valibot.ts' 'bun src/zod3.ts' 'bun src/zod4.ts' 'bun src/zod-mini.ts'",
49-
"bench:node": "hyperfine --runs 10 'node dist/arktype.js' 'node dist/valibot.js' 'node dist/zod3.js' 'node dist/zod4.js' 'node dist/zod-mini.js'",
50-
"bench:node:strip": "hyperfine --runs 10 'node --experimental-strip-types src/arktype.ts' 'node --experimental-strip-types src/valibot.ts' 'node --experimental-strip-types src/zod3.ts' 'node --experimental-strip-types src/zod4.ts' 'node --experimental-strip-types src/zod-mini.ts'",
48+
"bench:bun": "hyperfine 'bun src/arktype.ts' 'bun src/valibot.ts' 'bun src/zod-mini.ts' 'bun src/zod3.ts' 'bun src/zod4.ts'",
49+
"bench:node": "hyperfine --runs 10 'node dist/arktype.js' 'node dist/valibot.js' 'node dist/zod-mini.js' 'node dist/zod3.js' 'node dist/zod4.js'",
50+
"bench:node:strip": "hyperfine --runs 10 'node --experimental-strip-types src/arktype.ts' 'node --experimental-strip-types src/valibot.ts' 'node --experimental-strip-types src/zod-mini.ts' 'node --experimental-strip-types src/zod3.ts' 'node --experimental-strip-types src/zod4.ts'",
5151
"build": "bun scripts/build.ts && echo build success",
5252
"check": "pnpm i && bun check:tsc && bun run build && bun check:once && echo check success",
53-
"check:once": "bun src/arktype.ts && bun src/valibot.ts && bun src/zod3.ts && bun src/zod4.ts && bun src/zod-mini.ts && echo check:once success",
53+
"check:once": "bun src/arktype.ts && bun src/valibot.ts && bun src/zod-mini.ts && bun src/zod3.ts && bun src/zod4.ts && echo check:once success",
5454
"check:repo": "repo-check && echo check:repo success",
5555
"check:tsc": "tsc --noEmit && echo check:tsc success"
5656
},

src/zod-mini.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ for (let i = 0; i < nbIterations; i++) {
3131

3232
type UserInput = InferInput<typeof userSchema>;
3333

34-
const userA = userSchema.parse({ name: "Jordan", age: 42 });
34+
const userA = userSchema.parse({ name: "Jordan" });
3535
checkUserA(userA);
3636

3737
function createUser(input: UserInput) {
3838
const result = userSchema.safeParse(input);
3939
if (!result.success) return userA;
4040
return result.data;
4141
}
42-
42+
// (╯°□°)╯︵ ┻━┻
43+
// Zod mini doesn't support "or" so a string is given for phone instead of a number like in other scripts
4344
const userB = createUser({ name: "Romain", age: 35, phone: "1234567890" });
4445
checkUserB(userB);
4546

@@ -49,7 +50,7 @@ for (let i = 0; i < nbIterations; i++) {
4950
}
5051

5152
console.log(
52-
`Zod 4 mini exec time for ${nbIterations} iterations :`,
53+
`Zod mini exec time for ${nbIterations} iterations :`,
5354
performance.now() - startTime,
5455
"ms"
5556
);

src/zod3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ for (let i = 0; i < nbIterations; i++) {
4444
}
4545

4646
console.log(
47-
`Zod3 exec time for ${nbIterations} iterations :`,
47+
`Zod v3 exec time for ${nbIterations} iterations :`,
4848
performance.now() - startTime,
4949
"ms"
5050
);

src/zod4.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ for (let i = 0; i < nbIterations; i++) {
4444
}
4545

4646
console.log(
47-
`Zod4 exec time for ${nbIterations} iterations :`,
47+
`Zod v4 exec time for ${nbIterations} iterations :`,
4848
performance.now() - startTime,
4949
"ms"
5050
);

0 commit comments

Comments
 (0)