Implement faster JSON.stringify()
function by myself.
It would be v3
update and its JSON string conversion speed would be much faster than v2
. The JSON string conversion speed would be 3x times faster than before (would be thoundands times faster when considering the optimizer plannig time). Also, the v3
does not utilize the JSON schema, but TypeScript type directly. Therefore, typescript-json
would be much safer than ordinary v2
.
typescript-json
is wrapper library of fast-json-stringify
, which can omit JSON schema definition, through the TypeScript Compiler API with transformer. In such reason, providing maximum convenience to users with minimum effort was possible, by developing an automatic JSON schema generator. Furthermore, as typescript-json
could define JSON schema automatically, dependent library like nestia
could build swagger.json
very easily.
However, as adopted fast-json-stringify
, typescript-json
is following its disadvantages, too.
At 1st, fast-json-stringify
is not fast.
The fast-json-stringify
requires too much time to constructing faster JSON string conversion function. It analyzes JSON schema at runtime, therefore optimizing the JSON string conversion plan time is required, and it outcomes below result. As you can see from the below graph, optimizer planning time makes fast-json-stringify
slower, than the native JSON.stringify()
, about hundres times. Even measuring only JSON conversion speed, fast-json-stringify
is slow too, when recursive relationshp comes.
However, if I change typescript-json
to optimize the JSON string conversion plan in the compilation level, through the TypeScript Compiler API with transformer, the conversion speed would not be slow by constructing the optimizer plan. Also, as typescript-json
knows exact type of the target data, optimizer plan would be much evolved than before.
Include optimizer planning time |
Only JSON string conversion time |
|
|
At 2nd, fast-json-stringify
is not enough stable.
fast-json-stringify
is utilizing ajv
library as dependency and the ajv
library occurs an error when composite structured object type comes. Even the ajv
is using non-standard feature $recursiveRef
, which does not defined in the JSONS schema. However, the $recursiveRef
be broken when complicated structure comes.
However, if I develop JSON string conversion logic by myself, such bug would not be occured.
At last, TypeScript type is much superior than the JSON schema.
Developing the JSON schema generator from the TypeScript type, I understood that how TypeScrip type is superior and how JSON schema is inferior, within framework of the TypeScript user. Therefore, when developing TypeScript library boosting up the JSON string conversion speed, using pure TypeScript type is much better.
For example, fast-json-stringify
and ajv
cannot understand the Object.toJSON()
method, because they don't know original types of parameters. With fast-json-stringify
, to solve the below bug, custom JSON string conversion must check all of the values whether they're a type of object and has a toJSON()
method or not, via recursive visit strategy. However, it would make JSON string conversion speed much slower.
However, if v3
comes, the toJSON()
methods would be exactly traced in the compilation level.