HomeDocsBlogRSS
I made 10x faster JSON.stringify() functions, even type safe

I made 10x faster JSON.stringify() functions, even type safe

Jeongho NamOriginal on DEV
#javascript#programming#typescript#opensource
// FASTER STRINGIFY FUNCTIONS export function stringify<T>(input: T): string; // unsafe but fastest export function assertStringify<T>(input: T): string; // throws TypeGuardError export function isStringify<T>(input: T): string | null; // wrong type be null // RUNTIME VALIDATORS ALLOWING SUPERFLUOUS PROPERTIES export function is<T>(input: unknown): input is T; // returns boolean export function assertType<T>(input: unknown): T; // throws TypeGuardError export function validate<T>(input: unknown): IValidation; // detailed reasons // DO NOT ALLOW SUPERFLUOUS PROPERTIES export function equals<T>(input: unknown): input is T; export function assertType<T>(input: unknown): T; export function validate<T>(input: unknown): IValidation;

Hello, I’m a developer of typescript-json , which can validate instance type by only one line, what I’d introduced as “1,000x faster runtime validator library” in here dev.to . In today, I came back with powerful JSON string conversion functions.

As you can see from the above code, the new JSON string conversion functions throw TypedGuardError or return null value when input value is different with target type T. One thing amazing is, those JSON string conversion functions are much faster than the native JSON.stringify() function despite of type validation process.

JSON string conversion speed

Of course, as this benchmark measured by myself, someone can doubt its objectivity.

Therefore, to ensure the 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 install and npm run benchmark commands after cloning typescript-json  repository.

How to use

If you want to utilize those type safe JSON functions, just install and import typescript-json . After that, call those functions like below. You don’t need to define any extra dedication like fast-json-stringify  which requires JSON schema definition. Only you need is only one line with pure TypeScript type.

Note that, TSON.assertStringify() function throws TypeGuardError exception and TSON.isStringify() function returns boolean value when parametric input value is different with the target type T. Of course, if input value is matched with the type T, just JSON string would be returned. Anyway, choose one of them depending on your purpose.

import TSON from "typescript-json"; interface IMember { name: string; age: number; } // NO PROBLEM const exact: IMember = { name: "someone", age: 0 }; TSON.assertStringify(exact); TSON.isStringify(exact); // WRONG TYPE const wrong: IMember = { name: "someone", age: false as any }; TSON.assertStringify(wrong); // throws TypeGuardError TSON.isStringify(wrong); // returns null

How can be such faster

AOT (Ahead of Time) compilation.

typescript-json  analyzes your code (type T) in the compilation level and generates optimized runtime code for JSON string conversion. Such optimized runtime code generation by analyzing source code is called AOT (Ahead of Time) compilation and it is the reason why typescript-json ’s JSON string conversion functions are much faster than the native JSON.stringify() function despite of type validation process.

In the same reason, validation functions of typescript-json  are much faster than other validation libraries. Only TypeBox  can be competitive and TypeBox  utilizes JIT (Just-in-time) compilation. Other libraries without special skills are extremely slow, and the gap can widen by thousands of times.

Someone may have question, “Why are you comparing with other validator libraries? Where’s the other JSON string converter library?” Unfortunately, there’s not any other library that can convert JSON string safely like typescript-json . Therefore, I had to compare with validator libraries. Please understand me and such circumstance.

is() function benchmark

assertType() function benchmark

Full Spec of TypeScript Type

typescript-json  supports full spec of TypeScript type.

Therefore, use TSON.assertStringify() and TSON.isStringify() functions enhance your program’s type safety with confidence. I hope those functions to be really useful for network systems using JSON data, especially for enhancing type safety.

ComponentsTSONTypeBoxajvio-tszodC.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
  • TSON: typescript-json
  • C.V.: class-validator
Released under the MIT License. Copyright 2022 - 2026 Samchon.