[Typia] 15,000x faster TypeScript Validator and its histories

Read Time:6 Minute, 31 Second

Series of TypeScript Compiler based Libraries

  1. I made 1,000x faster TypeScript validator library
  2. I made 10x faster JSON.stringify() functions, even type safe
  3. Nestia, super-fast validator decorators for NestJS
  4. Automatic React components generator from TypeScript type

Renamed to Typia

Hello, I’m developer of typescript-json typia.

In nowadays, I’ve renamed typescript-json library to typia, because the word “JSON” no more can represent it. Key feature of typia had been changed from faster JSON stringify function to superfast validation function. Furthermore, typia has started supporting Protocol Buffer (in alpha version, not regular feature yet).

Introducing such package renaming from typescript-json to typia, I will tell you which changes had been occured during six months. Lots of interesting features and improvements have been happened.

What typia is:

typia is a transformer library of TypeScript, supporting below features:

  • Super-fast Runtime Validators
  • Safe JSON parse and fast stringify functions
  • JSON schema generator

All functions in typia require only one line. You don’t need any extra dedication like JSON schema definitions or decorator function calls. Just call typia function with only one line like typia.assert<T>(input).

Also, as typia performs AOT (Ahead of Time) compilation skill, its performance is much faster than other competitive libaries. For an example, when comparing validate function is() with other competitive libraries, typia is maximum 15,000x times faster than class-validator.

Became 15,000x faster runtime validator library

Enter fullscreen mode Exit fullscreen mode

Do you remember? About a month ago, I wrote an article about TypeBox and said “I found faster vlidator library than me (in some cases)”. During a month, I’d tried to understand and study the reason why.

During the studies, I found the reason why. The secret was on inlining.

When validating an instance type, typia generates functions per object type. Therefore, when a type is related with 8 object types, 8 internal functions would be generated.
However, typebox let user to decide whether to make validate function for an object type or not (by $recursiveRef flag).

Benchmark code about typebox was written by typebox author himself.

Looking at below code, you may understand what the inlining means and how typia and typebox are different. typia is generating functions per object type (Box3D and Point3d), but typebox does not make any internal function but inline all of them.

Enter fullscreen mode Exit fullscreen mode

As you know, function calls have their own cost. Therefore, inlining may be faster than function call. However, inlining is not always faster then function call, and there is a section where performance is reversed between function calls and inlining.

typebox let users to determine by themselves through JSON schema definition, but typia can’t do it, because typia generates runtime validator function automatically by only one line statement; typia.assert<T>(input).

Enter fullscreen mode Exit fullscreen mode

Therefore, it is necessary for me to compare the pros and cons of function call and inlining, and to select an algorithm at an appropriate level. I’d changed typia code to use such inlining skill in special cases and the result was typia became 15,000x faster validator than class-validator.

From now on, typia is the fastest runtime validator library.

[Typia] 15,000x faster TypeScript Validator and its histories

Measured on Intel i5-1135g7, Surface Pro 8

New Features

More Types

Enter fullscreen mode Exit fullscreen mode

After introducing typia (typescript-json at that time), many dev.to users (maybe?) started using it and wrote lots issues for bug reportings or new feature suggestions, too. Resolving issues for six months, typia became much stronger for TypeScript types.

For six months of improvements, typia could support template literal types and built-in class types like Uint8Array. Sometimes, bug from horrible types like “Array rest parametrized tuple type” or “endless recursive and conditional types” had been reported.

Anyway, I’d enhanced and resolved all of those issues, and now I say that “typia supports every TypeScript type, with confidence. Of course, the word “every TypeScript type” could be broken in any time, but it is not now (maybe).

Comment Tags

Some users requested typia to support much more types even TypeScript does not support.

To response to their inspirations, I’d studied solution for a while and found a good way at the right time. It is extending typia‘s spec through comment tags. For an example, JavaScript does not support integer type, but typia can validate the integer type through @type int comment tag.

Below is an example using such comment tags. Although those comment tags are written as a comment, but they’re compile time safe. If wrong grammered comment tags being used, typia will generate compilation error, therefore no need to worry about runtime error by mistakes.

Enter fullscreen mode Exit fullscreen mode

Preparing Protocol Buffer

Enter fullscreen mode Exit fullscreen mode

In nowadays, I am developing Protocol Buffer features.

ThoseT features are not published as stable version yet, but you can experience them through dev version. Also, you can read detailed manual about those Protocol Buffer features in Guide Documents – Protocol Buffer of typia.

Enter fullscreen mode Exit fullscreen mode

Those features are suggested by my neighbor TypeScript backend developer who is suffering from Protocol Buffer development. Although there’re some libraries supporting Protocol Buffer in TypeScript, but they’re not so convenient.

Therefore, he suggested them hoping to implement Protocol Buffer features very easily like other typia functions can be done with only one line. Listening his opinion and sympathizing with necessity, and it seems to make typia more famous, I’ve accepted his suggestion.

Just wait a month, then you TypeScript developers can implement Protocol Buffer data very easily, than any other language. Also, let’s make typia more famous!

Nestia – boosted up validation decorator

Nestia is a helper library set for NestJS, supporting below features:

  • @nestia/core: 15,000x times faster validation decorator using typia
  • @nestia/sdk: evolved SDK and Swagger generator for @nestia/core
  • nestia: just CLI (command line interface) tool

Developing typia and measuring performance benchmark with other competitive validator libraries, I’ve known that class-validator is the slowest one and its validation speed is maximum 15,000x times slower than mine, typia.

However, NestJS, the most famous backend framework in TypeScript, is using the slowest class-validator. Therefore, some TypeScript developers have identified that they’re using such slowest validator on their backend system (by my previous dev.to article), and requested me to support new validation decorator for NestJS.

In response to their desire, I’ve made a new library @nestia/core. It provides 15,000x faster validation decorator than ordinary NestJS validator decorator using class-validator.

Enter fullscreen mode Exit fullscreen mode

However, as NestJS can generate swagger documents only when using the slowest class-validator decorator, I had to make a new program @nestia/sdk, which can generate swagger documents from @nestia/core decorator.

For reference, @nestia/sdk also can generate SDK (Software Development Kit) library for client developers, by analyzing backend server codes in the compliation level. If I write next article in here dev.to, subject of the new article would be this nestia and SDK library.

In fact, I had developed nestia for a long time. However, previous nestia had focused only on SDK library generation. Before typia, there had not been any consideration about developing faster validation decorator.

Therefore, only @nestia/core is the new library made by requirements from TypeScript backend developers. @nestia/sdk is just a little bit evolved version of previous nestia, to support @nestia/core.

SDK library generated by @nestia/sdk