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 | |||||
|---|---|---|---|---|---|---|---|---|---|---|
sury | 11.0.0 | 333.97K | JIT | Code snippet(Setup code is not benchmarked) | 70 ns | Code snippet(Setup code is not benchmarked) | 94 ns | |||
io-ts | 2.2.22 | 1.94M | None | Code snippet(Setup code is not benchmarked) | 70 ns | Code snippet(Setup code is not benchmarked) | 90 ns | |||
zod/mini | 4.6.5 | 209.22M | JIT | Code snippet | 108 ns | Code snippet | 118 ns | |||
zod | 4.6.5 | 209.22M | JIT | Code snippet | 112 ns | Code snippet | 124 ns | |||
zod/mini (compile) | 4.6.5 | 209.22M | JIT | Code snippet | 115 ns | Code snippet | 130 ns | |||
zod (compile) | 4.6.5 | 209.22M | JIT | Code snippet(Setup code is not benchmarked) | 126 ns | Code snippet(Setup code is not benchmarked) | 121 ns | |||
effect | 3.22.2 | 24.53M | None | Code snippet | 256 ns | Code snippet | 299 ns | |||
effect (unknown) | Accepts unknown valuesThis codec allows unknown input, requiring extra validation. | 3.22.2 | 24.53M | None | Code snippet | 259 ns | Code snippet | 302 ns | ||
effect@rc | 4.0.0-rc.112 | 24.53M | None | Code snippet | 277 ns | Code snippet | 307 ns | |||
effect@rc (unknown) | Accepts unknown valuesThis codec allows unknown input, requiring extra validation. | 4.0.0-rc.112 | 24.53M | None | Code snippet | 279 ns | Code snippet | 296 ns | ||
typebox | Accepts unknown valuesThis codec allows unknown input, requiring extra validation. | 1.3.31 | 9.44M | JIT | Code snippet(Setup code is not benchmarked) | 3 μs | Code snippet(Setup code is not benchmarked) | 3 μs | ||