fastify / fastify/fast-json-stringify
Empty spaces make JSON string conversion faster
- Dominant language
- JavaScript
- Stars
- 3.7k
- Forks
- 226
- Avg merge
- 1d 59m
- Merged PRs (30d)
- 3
Description
### 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
```typescript
"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
```typescript
"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;
```
Contributor guide
Assessment
This issue has not been assessed yet.