Gathering detailed insights and metrics for @darkgl/slow-json-stringify
Gathering detailed insights and metrics for @darkgl/slow-json-stringify
npm install @darkgl/slow-json-stringify
Typescript
Module System
Min. Node Version
Node Version
NPM Version
69.8
Supply Chain
99.3
Quality
77.1
Maintenance
100
Vulnerability
100
License
TypeScript (51.48%)
JavaScript (46.73%)
Shell (1.79%)
Total Downloads
184
Last Day
3
Last Week
7
Last Month
66
Last Year
184
151 Commits
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
2.1.1
Package Id
@darkgl/slow-json-stringify@2.1.1
Unpacked Size
25.13 kB
Size
8.71 kB
File Count
25
NPM Version
10.9.0
Node Version
23.1.0
Publised On
05 Nov 2024
Cumulative downloads
Total Downloads
Last day
200%
3
Compared to previous day
Last week
-53.3%
7
Compared to previous week
Last month
371.4%
66
Compared to previous month
Last year
0%
184
Compared to previous year
The fastest JSON stringifier
SJS
shows a significant increase in performance over both native JSON.stringify
and fast-json-stringify
.
For some use cases (dealing with long text), it performs 21000% faster than both native and fast-json-stringify
.
NOTE: Support for undefined properties has been added from 1.0.1. SJS
is now production ready.
Checkout benchmarks.
SJS
is fully compatible with both Node.js and the browser 🎉🎉
Node:
1npm install slow-json-stringify
On the browser:
1<script src="https://unpkg.com/slow-json-stringify/dist/sjs.umd.js"></script>
Why SJS
is the fastest stringifier?
The traditional approach consists in serializing every property taken singularly.
SJS
uses a different approach to serialization.
Preparation:
Serialization:
It is faster simply because it performs a lot less work.
SJS
require some setup work if compared to native JSON.stringify
.
But, if you are dealing with json with a fixed structure SJS
will save you a ton of time.
Especially when the payload grows. And incredibly when serializing json with long text inside (think of a blog article or a product description.
Moreover, SJS
makes possible the serialization of types that are not natively supported by JSON.stringify
thanks to custom serializers.
note: SJS
won't perform any escaping as you usually won't need it in small payloads. If you are working with big text, it could be of very little effort to store in your db an already escaped text.
However, SJS
provides a little utility for your escaping needs.
escape
uses a default regex if no additional regex is provided.
default regex string:
1/\n|\r|\t|\"|\\/gm
You can use escape
like the following:
1const { escape } = require('slow-json-stringify'); 2 3// If you don't pass any additional regex, a default one will be used. 4const escaper = escape(); 5 6escaper('This is "funny"'); // This is \"funny\" 7 8// You can pass any regex you want for your escaping strategy. 9const customEscaper = escape(/\"/gm); 10 11customEscaper('This is "funny"'); // This is \"funny\" 12
We all know that there are three kinds of lies..
Lies, damned lies.. and benchmarks.
Remember to test if SJS
could be a real improvement for your use case.
Because there are times when the performance advantages with the added drawbacks could not be worth it.
Every benchmark is replicable on your own machine. To run your tests:
cd benchmark
.run.sh
script chmod +x ./run.sh
../run.sh >> benchmark.md
The benchmarks were performed on a Dell Xps 15 9550.
Checkout benchmarks here
The schema creation happens thanks to the attr
helper exported from the main bundle.
1const { attr } = require('sjs');
1attr(type, serializer?)
The attr
helper natively supports the following types:
string
number
boolean
null
array
, dynamic array with simple structure, in this scenario native JSON.stringify
will be used. As there are no real performance advantages.The serialization of any other type is possible thanks to custom serializers.
For a correct stringification of your json payload, a correct schema is mandatory. Defining a schema is pretty handy and not verbose at all.
1const { sjs } = require('slow-json-stringify'); 2 3// schema definition 4const stringify = sjs({ 5 a: attr('string'), 6 b: attr('number'), 7 c: attr('boolean'), 8}); 9 10// then you can stringify anything with that structure. 11stringify({ 12 a: 'world', 13 b: 42, 14 c: true, 15}); 16 17// {"a":"world","b":42,"c":true} 18
When stringifying simple array JSON.stringify
will be internally used.
1const { sjs } = require('slow-json-stringify'); 2 3// schema definition 4const stringify = sjs({ 5 a: attr('array'), 6}); 7 8// then you can stringify anything with that structure. 9stringify({ 10 a: [1, 2, 3, true, 'world'], 11}); 12 13// {"a":[1,2,3,true,"world"]} 14
The attr
helper accepts an additional sjs
schema for array
properties.
1const { sjs } = require('slow-json-stringify'); 2 3// schema definition 4const stringify = sjs({ 5 a: attr('array', sjs({ 6 b: array('string'), 7 c: array('number'), 8 })) 9}); 10 11// then you can stringify anything with that structure. 12stringify({ 13 a: [{ 14 b: 'ciao1', 15 c: 1, 16 }, { 17 b: 'ciao2', 18 c: 2, 19 }, { 20 b: 'ciao3', 21 c: 3, 22 }, { 23 b: 'ciao4', 24 c: 4, 25 }], 26}); 27 28// {"a":[{"b":"ciao1","c":1},{"b":"ciao2","c":2},{"b":"ciao3","c":3},{"b":"ciao4","c":4}]} 29
Defining schemas with nested objects is pretty straightforward.
1const { sjs } = require('slow-json-stringify'); 2 3// schema definition 4const stringify = sjs({ 5 a: { 6 b: { 7 c: attr('string'), 8 }, 9 }, 10 d: { 11 e: attr('number'), 12 }, 13}); 14 15stringify({ 16 a: { 17 b: { 18 c: 'hello', 19 }, 20 }, 21 d: { 22 e: 42, 23 }, 24}); 25 26// {"a":{"b":{"c":"hello"}},"d":{"e":42}} 27
The attr
helper accepts a serializer function. The serializer function gets invoked with the real value that should be stringified.
1serializer(rawValue)
Property serializers are useful to perform custom serialization on any provide type not natively supported even by JSON.stringify
(Dates, regular expressions).
They can be used also to skip property serializarion when returning undefined
.
1const { sjs } = require('slow-json-stringify'); 2 3// schema definition 4const stringify = sjs({ 5 a: attr('number', (value) => { 6 if (value > 10) { 7 return value; 8 } 9 10 return undefined; 11 }) 12}); 13 14stringify({ a: 20 }); 15// {"a":20} 16 17stringify({ a: 5 }); 18// {} 19
use case: Serialization of any type.
1// DATES 2const stringify = sjs({ 3 date: attr('string', (value) => value.toLocaleString()), 4}); 5 6// REGEXP 7const stringify = sjs({ 8 regexp: attr('string', (value) => value.toString()), 9});
use case: Customize payloads based on access rights.
1const stringify = sjs({ 2 publicProp: attr('string'), 3 restrictedProp: attr('string', (value) => isAdmin ? value : undefined), 4});
use case: Value formatting
1const stringify = sjs({ 2 prop: attr('string', (value) => value.toUpperCase()), 3});
param | type | required | default | spec |
---|---|---|---|---|
schema | object | yes | undefined | Schema that defines the stringification behavior. |
param | type | required | default | spec |
---|---|---|---|---|
type | string | yes | undefined | Type of the property. |
serializer | function | no | identity | Function used for serializing / validating properties. |
param | type | required | default | spec |
---|---|---|---|---|
regex | Regular Expression | no | default regex | regex used to escape text |
MIT.
No vulnerabilities found.
No security vulnerabilities found.