Skip to content

Commit

Permalink
Merge pull request #267 from samchon/features/is-stringify
Browse files Browse the repository at this point in the history
Close #266 - add `isStringify()` function
  • Loading branch information
samchon authored Oct 25, 2022
2 parents cf7f4ab + c355e65 commit a97181c
Show file tree
Hide file tree
Showing 190 changed files with 2,292 additions and 337 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
root: true,
plugins: [
"@typescript-eslint"
"@typescript-eslint",
"deprecation",
],
extends: [
"plugin:@typescript-eslint/recommended",
Expand Down Expand Up @@ -34,12 +35,12 @@ module.exports = {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/prefer-as-const": "error",
"deprecation/deprecation": "error",
}
}
]
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ TSON.assertEquals<T>(input); // throws exception
TSON.validateEquals<T>(input); // archives all errors

// APPENDIX FUNCTIONS
TSON.application<[T, U, V], "ajv">(); // JSON schema application generator
TSON.assertStringify<T>(input); // 3x faster stringify() + assertType()
TSON.stringify<T>(input); // 5x faster JSON.stringify()
TSON.assertStringify<T>(input); // assertType() + stringify()
TSON.isStringify<T>(input); // is() + stringify()
TSON.application<[T, U, V], "ajv">(); // JSON schema application generator
```

`typescript-json` is a transformer library providing JSON related functions.
Expand Down Expand Up @@ -263,24 +264,26 @@ The extreme different is shown in the "ultimate union" type, when validating [JS
### Fastest JSON String Converter
```typescript
export function stringify<T>(input: T): string;
export function assertStringify<T>(input: T): string;
export function stringify<T>(input: T): string; // do not validate type (danger)
export function assertStringify<T>(input: T): string; // throws TypeGuardError
export function isStringify<T>(input: T): string | null; // null when wrong type

export function createStringify<T>(): (input: T) => string;
export function createAssertStringify<T>(): (input: T) => string;
export function createIsStringify<T>(): (input: T) => string | null;
```

Super-fast JSON string conversion function.

When you call `TSON.stringify()` function instead of the native `JSON.stringify()`, the JSON conversion time would be 5x times faster. Also, you can perform such super-fast JSON string conversion very easily, by only one line: `TSON.stringify<T>(input)`.

If you want to validate the input type at the same time, you can choose `TSON.assertStringify<T>(input)` function instead. The function calls `TSON.assertType()` before converting to the JSON string. Of course, its conversion speed would be cut in half, but it would be much safer than the native `JSON.stringify()`.
If you want to validate the input type at the same time, you can choose `TSON.isStringify<T>(input)` or `TSON.assertStringify<T>(input)` functions instead. Those function calls `TSON.is()` or `TSON.assertType()` function before converting to the JSON string. Of course, its conversion speed would be reduced, but it would be much safer than the native `JSON.stringify()`.

Comparing performance, `typescript-json` is about 5x times faster when comparing only JSON string conversion time. If compare optimizer construction time, `typescript-json` is even 10,000x times faster.
Comparing performance, `typescript-json` is about 5x times faster than the native `JSON.stringify()` function.

![JSON conversion speed on each CPU](https://user-images.githubusercontent.com/13158709/197602385-7dfea7a6-48ef-432a-92b1-71d011c0c290.png)
![JSON conversion speed on each CPU](https://user-images.githubusercontent.com/13158709/197731958-f20157b2-2e31-4e24-a27f-3ae9a8e565bb.png)

> Measured on Intel it-1135g7, Surface Pro 8
> Measured on Intel AMD-5800H
### JSON Schema Generation
```typescript
Expand Down
7 changes: 7 additions & 0 deletions benchmark/features/benchmark_stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ObjectSimple], "ajv">(),
),
Expand All @@ -44,6 +45,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ObjectHierarchical], "ajv">(),
),
Expand All @@ -55,6 +57,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ObjectRecursive], "ajv">(),
),
Expand All @@ -66,6 +69,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ObjectUnionImplicit], "ajv">(),
),
Expand All @@ -81,6 +85,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ArrayHierarchical], "ajv">(),
),
Expand All @@ -92,6 +97,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ArrayRecursive], "ajv">(),
),
Expand All @@ -103,6 +109,7 @@ const stringify = () => [
{
"TSON.stringify()": (input) => TSON.stringify(input),
"TSON.assertStringify()": (input) => TSON.assertStringify(input),
"TSON.isStringify()": (input) => TSON.isStringify(input)!,
"fast-json-stringify": build(
TSON.application<[ArrayRecursiveUnionExplicit], "ajv">(),
),
Expand Down
6 changes: 6 additions & 0 deletions benchmark/internal/StringifyBenchmarker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export namespace StringifyBenchmarker {
name: string;
"TSON.stringify()": number;
"TSON.assertStringify()": number;
"TSON.isStringify()": number;
"fast-json-stringify": number | null;
"JSON.stringify()": number;
}
export interface IParameters<T> {
"TSON.stringify()": (input: T) => string;
"TSON.assertStringify()": (input: T) => string;
"TSON.isStringify()": (input: T) => string;
"fast-json-stringify": null | ((input: T) => string);
}

Expand All @@ -33,12 +35,16 @@ export namespace StringifyBenchmarker {
suite.add("TSON.assertStringify()", () =>
parameters["TSON.assertStringify()"](data),
);
suite.add("TSON.isStringify()", () =>
parameters["TSON.isStringify()"](data),
);

return () => {
const output: IOutput = {
name,
"TSON.stringify()": 0,
"TSON.assertStringify()": 0,
"TSON.isStringify()": 0,
"JSON.stringify()": 0,
"fast-json-stringify": null,
};
Expand Down
Loading

0 comments on commit a97181c

Please sign in to comment.