/like-json

Stringify at perfect performance. +1200%

Primary LanguageJavaScriptMIT LicenseMIT

like-json

Stringify at perfect performance. +1200%

tested with jest

Benchmark it yourself: https://jsperf.com/likejson

Install

Node.js:

npm i like-json

Browser:

<script src="https://unpkg.com/like-json/index.min.js"></script>

Features

Methods

like.json(obj: Object|Array|Any, options: Object): Function
like.stringify(obj: Object|Array|Any, uniqueId: Number, options: Object): String

Examples

json

const like = require('like-json');

let stringify = like.json({ msg: '' });

let string1 = JSON.stringify({ msg: 'account created' });
let string2 = stringify({ msg: 'account created' });

console.log(string1 === string2); // true

stringify

const like = require('like-json');

let string1 = JSON.stringify({ msg: 'account created' });
let string2 = like.stringify({ msg: 'account created' }, 1);

console.log(string1 === string2); // true

How it works?

There is no processing, so same than a simple concatenation:

console.log(stringify.toString());
// ->
function anonymous (o) {
  return '{"msg":"' + o.msg + '"}';
}

Because that there is a conflict with double quotes:

{ msg: 'account "user" created' } // object
{"msg":"account \"user\" created"} // JSON.stringify
{"msg":"account "user" created"} // like.stringify

You can use single quotes, in that way avoid escape which reduces performance.
If really want to use double quotes then can use options:

let options = { encode: true };
let string1 = JSON.stringify({ msg: 'account "user" created' });
let string2 = like.stringify({ msg: 'account "user" created' }, 2, options);
console.log(string1 === string2); // true

Tests

npm test

Inside of stringify.test.js can see more info.

License

Code released under the MIT License.