unsafe-json
v1.0.3
Published
unsafe json serializer, but fast.
Downloads
7
Maintainers
Readme
unsafe-json
unsafe json serializer, but fast.
Installation
npm i unsafe-json -S
Usage
import { build, parse } from 'unsafe-json';
const obj = {
name: 'john',
age: 23,
married: false,
skills: ['nodejs', 'python'],
location: {
country: 'china',
city: 'beijing'
}
};
const schema = {
name: 'string',
age: 'number',
married: 'boolean',
skills: ['string'],
location: {
country: 'string',
city: 'string'
}
};
const stringify = build(schema);
const str = stringify(obj);
console.log(str);
console.log(parse(str));
Api
.build(schema: any, strict = true): (obj: any) => string
to squeeze a little bit more performance, set strict = false
, it will not dispose type of undefined
.parse(jsonStr: string)
like JSON.parse
Performance
tests on my PC: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
- simple object, ~8X faster (strict=false)
{"a":1,"b":"hello","c":{"x":1,"y":"zzzzzz"}}
------- performance comparation -------
native stringify: 68.610ms
unsafe stringify: 8.842ms
native parse: 103.864ms
unsafe parse: 42.718ms
- object with array, ~3X faster
{"a":1,"b":"hello","c":{"x":1,"y":"zzzzzz"},"d1":["asd","aaaaaa","asdasdasd"],"d":[{"x":1,"y":"zzzzzz"},{"x":1,"y":"zzzz"},{"x":1,"y":"z"}]}
------- performance comparation -------
native stringify: 188.564ms
unsafe stringify: 66.726ms
native parse: 252.536ms
unsafe parse: 72.011ms
Todo
- [ ] xxx
License
MIT