DEV Community

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

Collapse
 
samchon profile image
Jeongho Nam • Edited

In another community, someone asked me the reason why such performance gap.

It's my answer and I also paste it here dev.to


"15,000x faster" is just a benchmark program result and such different is not weird considering principle of v8 optimization. It is enough reasonable and descriptable.

  1. typia performs AOT compilation through TypeScript API
  2. ajv and typebox performs JIT compilation through eval() function
  3. class-validator abuses for in statement and dynamic [key, value] allocation in every step

V8 engine optimizes object construction by converting to a hidden class and avoid hash map construction for taking advantages of static class definition. However, if for in statement or dynamic key allocation being used, v8 cannot optimize the object.

The secret of extremely slow validation speed of class-validator is on there. class-validator utilizes the un-optimizable in every process.

  1. for in statement on metadata when iterating raw data to transform
  2. dynamic allocation when transforming to a class instance
  3. dynamic statement on metadata (of decorator)
  4. for in statement when validation

For reference, ajv and typebox are using JIT compilation, generating optimized code in runtime through eval() function (or new Function(string) statement). In v8 engine, priority of optimization is the lowest and it is the principle reaon why typia is faster than such ajv and typebox libraries.

Another reason is how to optimize object accessment. You can see the detailed story about it from below link (this article)