Comparison With Other Libraries
What sets PureParse apart from other validation libraries is the freedom it gives to the user to choose between type inference and explicit type declarations.
For a more detailed comparison, see the overview.
Performance Benchmarks
PureParse is benchmarked against other validation libraries; see https://moltar.github.io/typescript-runtime-type-benchmarks/.
Here are some results as measured on October, 2024:
- Chip: Apple M1 Pro
- Memory: 16 GB
IMPORTANT
The benchmark measures a very specific use case: the performance of parsing/validating a small object with required properties, one of which is another object:
IMPORTANT
Not all parsers in the tables below have the same feature set, so the benchmarks are not entirely comparable.
Loose Assertion
Loose assertion means that the function checks if the object conforms to the schema, and does not invalidate data that contains unknown properties. This corresponds to PureParse's guard functions:
Function | Mops/s | Relative to PureParse |
---|---|---|
PureParse* | 75.5 | 100% |
PureParse | 5.7 | 7.5% |
Zod | 1.12 | 1.4% |
io-ts | 3.5 | 4.7% |
Ajv* | 49 | 65% |
Yup* | 84 | 120% |
Typia** | 101 | 136% |
* Just-in-time (JIT) compilation
** Ahead-of-time compilation
Safe Parsing
Safe parsing means that a copy of the parsed data is returned, which contains only those properties that were declared. This protects against prototype pollution, and corresponds to PureParse's parsers.
Function | Mops/s | Relative to PureParse |
---|---|---|
PureParse* | 28.4 | 100% |
PureParse | 4.1 | 5.5% |
Zod | 1.13 | 3.9% |
io-ts† | — | — |
Ajv*† | — | — |
Yup* | 81.4 | 286% |
Typia** | 57.7 | 203% |
* Just-in-time (JIT) compilation
** Ahead-of-time compilation
†No benchmark data available—the operation might not be supported by the library
Size Comparison
Library | Minified + Zipped |
---|---|
PureParse | 5 kB |
Zod | 21 kB |
Joi | 236 kB |
Ajv | 32 kB |
Yup | 60 kB |
Typia* | — |
*Typia is a compiler
NOTE
Even though other libraries might be much larger, the final contribution to the bundle size will depend on whether the library is tree-shakeable, how tree-shakeable it is, and how many features of the library are used in the application.
Zod
In Zod, it is not possible to annotate the schemas with a type alias—you are instead supposed to derive your types from the schemas, which has the drawback that it couples your type aliases to Zod.
Zod is more difficult to extend with custom validation logic. (In PureParse, you just implement a function of the Parse<?>
or Guard<?>
type.)
Joi
In Joi, you can annotate the schemas with a type alias, but it does not validate the schema completely. As such, there is no real type safety.
Joi cannot infer the type from a schema.
Ajv
Ajv is a JSON validator and conforms to the JSON Schema specification. However, the JSON schema syntax is verbose, does not interoperate well with TypeScript, and does not lend itself well to customization of validation logic.
Yup
Yup cannot correctly infer the type from schemas—you can infer something, but it is not 100% correct.
Yup does allow asynchronous validation.
Typia
Typia is a compiler that generates validation logic based on type aliases. The resulting code is fast, and the boilerplate is minimal, but customization happens in a non-standard comments and so-called type-tags—also non-standard. In PureParse, this happens in regular JavaScript code.
When working with immutable data structures, PureParse can outperform Typia thanks to memoization.
Typia does not have error messages.