Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 8, 2022 04:37 pm GMT

I made 1,000x faster TypeScript Validator Library

Hello, I'm developer of typescript-json and have measure a benchmark comparing performance with other competitive libraries. By the benchmark, my library typescript-json has been turned out that maximum 1,000x times faster than zod.

Being proud of such performance enhancement, I hope many TypeScript developers to adapt my library. Advantage of typescript-json is not only better performance, but also easy usage exists. typescript-json does not require any extra schema definition and just requires only one line with pure TypeScript Type. Thus, what about using my library typescript-json instead of io-ts or zod?

// ALLOW SUPERFLUOUS PROPERTIESexport function is<T>(input: T): boolean; // true or falseexport function assertType<T>(input: T): T; // throws `TypeGuardError`export function validate<T>(input: T): IValidation; // detailed reasons// DO NOT ALLOW SUPERFLUOUS PROPERTIESexport function equals<T>(input: T): boolean;export function assertEquals<T>(input: T): T;export function validateEquals<T>(input: T): IValidation;

p.s) Of course, as this benchmark is being measured by me, someone can doubt objectivity. Therefore, to ensure objectivity, I diclose all code used in the benchmark. Below codes are being used in the benchmark. You also can run the benchmark program by running npm run benchmark command after downloding this typescript-json.

is() function

export function is<T>(input: T): boolean;

Is Function Benchmark

Measured on Intel i5-1135g7, Surface Pro 8

I hadn't known that zod is extremely slow until running on this benchmark program. io-ts is also slower than typescript-json and ajv about 8x times, but its slow performance looks like nothing comparing with zod.

More amazing thing is, ajv requires JSON schema definition for validating, but it can't validate the JSON schema type itself. Although other libraries io-ts and zod also failed to validating the JSON schema definition, isn't ajv different with them? Furthermore, when complicate type comes, ajv dies with runtime error in the most case.

validate() function

export function validate<T>(input): IValidation;export interface IValidation {    success: boolean;    errors: IValidation.IError[];}export namespace IValidation {    export interface IError {        path: string;        expected: string;        value: any;    }}

Super-fast runtime validator

Measured on Intel i5-1135g7, Surface Pro 8

Unfortunately, unable to measure ajv's performance because it does not support the validate function describing detailed error reasons.

Anyway, although zod is the slowest library even in the validate() function, performance gap with other libraries are dramatically(?) reduced. Also, performance gap ratio with typescript-json with io-ts are sometimes increased and sometimes reduced.

By the way, when measuring of zod, I sometimes doubted myself, "Have I taken a mistake?". But there was not any mistake and I wondered why zod is such slow. To analyze the reason why, I'd read source code of zod and this is my conclusion.

Although zod is the slowest validator, error reasons are more detailed and exact than io-ts. Therefore, its slow performance on validate() function is understandable.

Also, zod hasn't implemented special logic for is() function and let user to re-use the validate() function. Therefore, when zod implements the independent is() function algorithm, I think its performance can overcome io-ts.

Full Spec of TypeScript Type

typescript-json is much faster and much easier to use than io-ts and zod. But it's not all the thing. typescript-json can validate full TypeScript Type spec. Below table is a list of supported spec sheet about competitive runtime validator libararies. As you can see from the below table, only typescript-json can validate complicate union types.


Original Link: https://dev.to/samchon/typescript-json-is-10-1000x-times-faster-than-zod-and-io-ts-8n6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To