Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty spaces make JSON string conversion faster #467

Open
2 tasks done
samchon opened this issue Jun 19, 2022 · 2 comments
Open
2 tasks done

Empty spaces make JSON string conversion faster #467

samchon opened this issue Jun 19, 2022 · 2 comments

Comments

@samchon
Copy link

samchon commented Jun 19, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

As you know, I've developed a wrapper library of this fast-json-stringify, which can perform by only one line. In nowadays, I'm trying to upgrade the JSON string conversion function by myself, to avoid the #417 error, not to use ajv and JSON schema definition at all.

Anyway, during the development, I've found an interesting thing. Empty spaced JSON string conversion function is about 2x times faster than non empy scaped function. Therefore, I report you such phenomenon. When I'd met such phenomenon accidently, I was confused. But now, I could understand why the empty spaces make JSON string conversion faster.

Empty spaces make JSON string to be logner and it reduces number of character array (char*) re-constructions.

From many experiments, I've found that using 8 spaces between object properties is the comon best. I'm experimenting to other types like array and tuple, but the 8 spaces are still effective.

Empty spaced

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tson_simple = void 0;
const src_1 = __importDefault(require("../../src"));
const tson_simple = (input) => (input => {
    var $string = src_1.default.stringify.string;
    var $tail = src_1.default.stringify.tail;
    const is = [
        input => true && (true && null !== input.scale && undefined !== input.scale && (true && null !== input.scale && typeof input.scale === "object" && is[1](input.scale))) && (true && null !== input.position && undefined !== input.position && (true && null !== input.position && typeof input.position === "object" && is[1](input.position))) && (true && null !== input.rotate && undefined !== input.rotate && (true && null !== input.rotate && typeof input.rotate === "object" && is[1](input.rotate))) && (true && null !== input.pivot && undefined !== input.pivot && (true && null !== input.pivot && typeof input.pivot === "object" && is[1](input.pivot))),
        input => true && (true && null !== input.x && undefined !== input.x && typeof input.x === "number") && (true && null !== input.y && undefined !== input.y && typeof input.y === "number") && (true && null !== input.z && undefined !== input.z && typeof input.z === "number")
    ];
    const stringify = [
        input => `{        "scale":${stringify[1](input.scale)},        "position":${stringify[1](input.position)},        "rotate":${stringify[1](input.rotate)},        "pivot":${stringify[1](input.pivot)}}`,
        input => `{        "x":${input.x},        "y":${input.y},        "z":${input.z}}`
    ];
    return stringify[0](input);
})(input);
exports.tson_simple = tson_simple;

Non-empty spaced

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tson_simple = void 0;
const src_1 = __importDefault(require("../../src"));
const tson_simple = (input) => (input => {
    var $string = src_1.default.stringify.string;
    var $tail = src_1.default.stringify.tail;
    const is = [
        input => true && (true && null !== input.scale && undefined !== input.scale && (true && null !== input.scale && typeof input.scale === "object" && is[1](input.scale))) && (true && null !== input.position && undefined !== input.position && (true && null !== input.position && typeof input.position === "object" && is[1](input.position))) && (true && null !== input.rotate && undefined !== input.rotate && (true && null !== input.rotate && typeof input.rotate === "object" && is[1](input.rotate))) && (true && null !== input.pivot && undefined !== input.pivot && (true && null !== input.pivot && typeof input.pivot === "object" && is[1](input.pivot))),
        input => true && (true && null !== input.x && undefined !== input.x && typeof input.x === "number") && (true && null !== input.y && undefined !== input.y && typeof input.y === "number") && (true && null !== input.z && undefined !== input.z && typeof input.z === "number")
    ];
    const stringify = [
        input => `{"scale":${stringify[1](input.scale)},"position":${stringify[1](input.position)},"rotate":${stringify[1](input.rotate)},"pivot":${stringify[1](input.pivot)}}`,
        input => `{"x":${input.x},"y":${input.y},"z":${input.z}}`
    ];
    return stringify[0](input);
})(input);
exports.tson_simple = tson_simple;
@mcollina
Copy link
Member

Good finding! Have you got a benchmark demonstrating this?

@samchon
Copy link
Author

samchon commented Jun 20, 2022

Used same program with you (benchmark):

  • Spaced: 1,491 %
  • Non-spaced: 989 %
  • typescript-json-stringify: 847 %
  • Native JSON.stringify(): 100 %

If you want to run the benchmark program:

git clone https://github.com/samchon/typescript-json -b v3.0
cd typescript-json
npm install
npm run benchmark

When running on Surface Pro 8 (i5-1135g4), be 1.8x times faster. However, the difference be shrinked in R7-5800H

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants