DEV Community 👩‍💻👨‍💻

Cover image for I made 1,000x faster TypeScript Validator Library
Jeongho Nam
Jeongho Nam

Posted on • Updated on

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 PROPERTIES
export function is<T>(input: T): boolean; // true or false
export function assertType<T>(input: T): T; // throws `TypeGuardError`
export function validate<T>(input: T): IValidation; // detailed reasons

// DO NOT ALLOW SUPERFLUOUS PROPERTIES
export function equals<T>(input: T): boolean;
export function assertEquals<T>(input: T): T;
export function validateEquals<T>(input: T): IValidation;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Components TSON ajv io-ts zod C.V.
Easy to use
Object (simple)
Object (hierarchical)
Object (recursive)
Object (union, implicit)
Object (union, explicit)
Object (additional tags)
Object (template literal types)
Object (dynamic properties)
Array (hierarchical)
Array (recursive)
Array (recursive, union)
Array (R+U, implicit)
Ultimate Union Type

Top comments (34)

Collapse
 
crobinson42 profile image
Cory Robinson

Suretype is an excellent library that folks forget to mention.

Collapse
 
dagnelies profile image
Arnaud Dagnelies

Thanks again for suggesting this library which I used in the end. Instead of deriving the validation from the types like here, they go the other way around. You define the validation rules, which can define more precise rules than mere type definitions, and it deduces the type directly. That way, you don't need the intermediate code generation step like in this article, you get more precise validation and full typing support. Truly the best of both worlds.

Collapse
 
marcussa profile image
Marcus S. Abildskov
Collapse
 
aderchox profile image
aderchox

It's not even packaged as an NPM module? (I know it can be npm installed from GitHub directly, but that's not nice...).

Collapse
 
dagnelies profile image
Arnaud Dagnelies

Interesting. I was just looking for a json/ts validation library, what a coincidence! Well, I might as well try this out. ;) By the way, as a humble developer, performance is nice but simplicity is key. Thanks for the lib.

Collapse
 
dagnelies profile image
Arnaud Dagnelies

Hmmm.... My first attempt failed. no transform has been configured. I guess the generation of schemas during the compilation step is missing. Btw, I'm using wrangler which is a toolchain of its own, so I have no idea how to incorporate the step.

Collapse
 
samchon profile image
Jeongho Nam

The wrangler is something like cra or vite? If you can configure webpack, then you can use this typescript-json following README article. If configuring custom webpack is too hard, I recommend to use vite instead because its configuration is the easiest one.

If you wanna detailed help or guide, you also can write an issue on the repo. Then I or another developer may help you.

Thread Thread
 
amenadiel profile image
Felipe Figueroa

Wrangler should delegate to whatever is executed by package.json's build script . Usually it's either esbuild or webpack.

Collapse
 
chema profile image
José María CL

wow! congrats!

Collapse
 
yongchanghe profile image
Yongchang He

Congrats!

Collapse
 
marcussa profile image
Marcus S. Abildskov

deepkit.io/library/type
Still a long way to go 😉

Collapse
 
samchon profile image
Jeongho Nam • Edited on

I know that library and also benchmarked it. However, it requires too much compilation time and does not support any complicate type like generic. I will report bug soon.

Collapse
 
marcussa profile image
Marcus S. Abildskov • Edited on

"too much compilation time" are you transpiling code on a potato? I've had no issues whatsoever.
"complicate type like generic" I've still had no issues whatsoever.

Anyway, it's like comparing a quantum computer to a potato 🤷‍♂️

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
cmcnicholas profile image
Craig McNicholas

Written by a guy who has never had to hand validate hundreds of dependant models, discriminated types and deeply nested model heirarchies.

Collapse
 
hakimio profile image
Tomas Rimkus

No, it's not "clean architecture". It's "Dunning–Kruger effect".

Collapse
 
h_sifat profile image
Muhammad Sifat Hossain • Edited on

Well, I'm still a junior, please don't judge me, I'm still learning! Whenever I'm doing my whole entity validation with joi, zod or something else, my business logic becomes coupled to it. And when I read the clean architecture book it says, that the core entities should not directly depend on anything. But I understand it's not a law.

Collapse
 
lioness100 profile image
Lioness100

You should benchmark against @sapphire/shapeshift!

Collapse
 
samchon profile image
Jeongho Nam

Welcome your PR.

Collapse
 
patroza profile image
Patrick Roza

What about more advanced runtime validations, like basic regex?

Collapse
 
samchon profile image
Jeongho Nam

github.com/samchon/typescript-json...

Is this what you want?

Collapse
 
patroza profile image
Patrick Roza • Edited on

That looks great. if you could combine this with branded types, so instead of email: string
you would have email: Email
And an Email type that would carry the email validation rules. And has eg type string & Brand<EmailBrand>

Collapse
 
dashmug profile image
Noel Martin Llevares

Can you use typescript-json to validate based on JSON schemas like what ajv does?

Collapse
 
samchon profile image
Jeongho Nam

Use TSON.application() function. It may what you want.

Collapse
 
rickhoro profile image
Rick Horowitz

Two questions...

  1. Have you benchmarked against yup?
  2. Do you plan to implement a resolver for react-hook-form? Thanks!!
Collapse
 
samchon profile image
Jeongho Nam • Edited on
  1. No plan to measure yup because it does not support even union type

  2. No plan to make it. But welcome if you make and publish it as your own.

By the way, I made a react library which requires only TypeScript type like below. It would be published after styling, clear API designing with documentation. This may be what you want.

Image description

Collapse
 
marcussa profile image
Marcus S. Abildskov
Collapse
 
crobinson42 profile image
Cory Robinson

@samchon do you plan to support ts to json-schema export in this library or an extension of this library?

Collapse
 
samchon profile image
Jeongho Nam

What about using TSON.application() function? It generates JSON schema.

Collapse
 
kostyatretyak profile image
Костя Третяк

What do you mean when you write, for example, typescript-json 28.579% and zod - 100%?

Collapse
 
samchon profile image
Jeongho Nam • Edited on

github.com/samchon/typescript-json...

Slowest feature as 100% and 0% means failure.

If you wanna detailed measurement, click above link. The number means that how many jobs have done in one second.

Collapse
 
thejaredwilcurt profile image
The Jared Wilcurt

My dead grandma's dead dog is faster than TypeScript. low bar

Collapse
 
fxrahul7 profile image
fxrahul7

Will it work with tRPC?

Collapse
 
samchon profile image
Jeongho Nam

I don't know about tRPC, but it may work if calling TSON function is possible

👋 Have You Posted on DEV Yet?

Head over to our Welcome Thread and tell us a bit about yourself!