Some schema libraries support two way conversion of data, often referred to as "encoding" and "decoding".
We benchmark the time taken to encode and decode a bigint to and from a string.
Copy to clipboardimport * as z from "zod"; const bigIntFromString = z.codec( z.string(), z.bigint(), { decode: (str) => BigInt(str), encode: (bigint) => bigint.toString(), }, ); bigIntFromString.encode( 1234567890123456789n, ); // "1234567890123456789" bigIntFromString.decode( "1234567890123456789", ); // 1234567890123456789n
Copy to clipboardimport * as z from "zod"; const bigIntFromString = z.codec(z.string(), z.bigint(), { decode: (str) => BigInt(str), encode: (bigint) => bigint.toString(), }); bigIntFromString.encode(1234567890123456789n); // "1234567890123456789" bigIntFromString.decode("1234567890123456789"); // 1234567890123456789n
Invalid data
We don't benchmark codecs with invalid data, as many libraries require the input to be correctly typed before passing it to the codec.
Optimizations
| Library | Version | /wk | Optimizations | Encode | Decode | |||||
|---|---|---|---|---|---|---|---|---|---|---|
io-ts | 2.2.22 | n/a | None | Code snippet(Setup code is not benchmarked) | 87 ns | Code snippet(Setup code is not benchmarked) | 122 ns | |||
@mionjs/run-types | 0.20.0 | n/a | Precompiled | Code snippet(Setup code is not benchmarked) | 87 ns | Code snippet(Setup code is not benchmarked) | 147 ns | |||
sury | 11.1.0 | n/a | JIT | Code snippet(Setup code is not benchmarked) | 88 ns | Code snippet(Setup code is not benchmarked) | 137 ns | |||
zod/mini | 4.6.7 | n/a | JIT | Code snippet | 149 ns | Code snippet | 160 ns | |||
zod/mini (compile) | 4.6.7 | n/a | JIT | Code snippet | 150 ns | Code snippet | 147 ns | |||
zod | 4.6.7 | n/a | JIT | Code snippet | 151 ns | Code snippet | 158 ns | |||
zod (compile) | 4.6.7 | n/a | JIT | Code snippet(Setup code is not benchmarked) | 162 ns | Code snippet(Setup code is not benchmarked) | 158 ns | |||
effect | 4.1.4 | n/a | None | Code snippet | 348 ns | Code snippet | 353 ns | |||
effect (unknown) | Accepts unknown valuesThis codec allows unknown input, requiring extra validation. | 4.1.4 | n/a | None | Code snippet | 356 ns | Code snippet | 359 ns | ||
typebox | Accepts unknown valuesThis codec allows unknown input, requiring extra validation. | 1.4.7 | n/a | JIT | Code snippet(Setup code is not benchmarked) | 6 μs | Code snippet(Setup code is not benchmarked) | 6 μs | ||