fast-json-stringify icon indicating copy to clipboard operation
fast-json-stringify copied to clipboard

Empty spaces make JSON string conversion faster

Open samchon opened this issue 2 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] 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;

samchon avatar Jun 19 '22 03:06 samchon

Good finding! Have you got a benchmark demonstrating this?

mcollina avatar Jun 19 '22 10:06 mcollina

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

samchon avatar Jun 20 '22 02:06 samchon