Gathering detailed insights and metrics for @edgeros/safe-stable-stringify
Gathering detailed insights and metrics for @edgeros/safe-stable-stringify
Gathering detailed insights and metrics for @edgeros/safe-stable-stringify
Gathering detailed insights and metrics for @edgeros/safe-stable-stringify
npm install @edgeros/safe-stable-stringify
Typescript
Module System
Node Version
NPM Version
71.7
Supply Chain
98.3
Quality
80.1
Maintenance
100
Vulnerability
100
License
Total Downloads
1,337
Last Day
1
Last Week
10
Last Month
53
Last Year
884
Minified
Minified + Gzipped
Latest Version
2.4.4
Package Id
@edgeros/safe-stable-stringify@2.4.4
Unpacked Size
29.38 kB
Size
6.78 kB
File Count
8
NPM Version
@edgeros/safe-stable-stringify@2.4.4
Node Version
16.15.0
Published on
Dec 16, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
400%
10
Compared to previous week
Last Month
55.9%
53
Compared to previous month
Last Year
95.1%
884
Compared to previous year
1
Safe, deterministic and fast serialization alternative to [JSON.stringify][]. Zero dependencies. ESM and CJS. 100% coverage.
Gracefully handles circular structures and bigint instead of throwing.
Optional custom circular values, deterministic behavior or strict JSON compatibility check.
The same as [JSON.stringify][].
value
{any}replacer
{string[]|function|null}space
{number|string}1const stringify = require('safe-stable-stringify') 2 3const bigint = { a: 0, c: 2n, b: 1 } 4 5stringify(bigint) 6// '{"a":0,"b":1,"c":2}' 7JSON.stringify(bigint) 8// TypeError: Do not know how to serialize a BigInt 9 10const circular = { b: 1, a: 0 } 11circular.circular = circular 12 13stringify(circular) 14// '{"a":0,"b":1,"circular":"[Circular]"}' 15JSON.stringify(circular) 16// TypeError: Converting circular structure to JSON 17 18stringify(circular, ['a', 'b'], 2) 19// { 20// "a": 0, 21// "b": 1 22// }
bigint
{boolean} If true
, bigint values are converted to a number. Otherwise
they are ignored. Default: true
.circularValue
{string|null|undefined|ErrorConstructor} Defines the value for
circular references. Set to undefined
, circular properties are not
serialized (array entries are replaced with null
). Set to Error
, to throw
on circular references. Default: '[Circular]'
.deterministic
{boolean} If true
, guarantee a deterministic key order
instead of relying on the insertion order. Default: true
.maximumBreadth
{number} Maximum number of entries to serialize per object
(at least one). The serialized output contains information about how many
entries have not been serialized. Ignored properties are counted as well
(e.g., properties with symbol values). Using the array replacer overrules this
option. Default: Infinity
maximumDepth
{number} Maximum number of object nesting levels (at least 1)
that will be serialized. Objects at the maximum level are serialized as
'[Object]'
and arrays as '[Array]'
. Default: Infinity
strict
{boolean} Instead of handling any JSON value gracefully, throw an
error in case it may not be represented as JSON (functions, NaN, ...).
Circular values and bigint values throw as well in case either option is not
explicitly defined. Sets and Maps are not detected! Default: false
1import { configure } from 'safe-stable-stringify' 2 3const stringify = configure({ 4 bigint: true, 5 circularValue: 'Magic circle!', 6 deterministic: false, 7 maximumDepth: 1, 8 maximumBreadth: 4 9}) 10 11const circular = { 12 bigint: 999_999_999_999_999_999n, 13 typed: new Uint8Array(3), 14 deterministic: "I don't think so", 15} 16circular.circular = circular 17circular.ignored = true 18circular.alsoIgnored = 'Yes!' 19 20const stringified = stringify(circular, null, 4) 21 22console.log(stringified) 23// { 24// "bigint": 999999999999999999, 25// "typed": "[Object]", 26// "deterministic": "I don't think so", 27// "circular": "Magic circle!", 28// "...": "2 items not stringified" 29// } 30 31const throwOnCircular = configure({ 32 circularValue: Error 33}) 34 35throwOnCircular(circular); 36// TypeError: Converting circular structure to JSON
[Circular]
(configurable).Number(5)
) are not unboxed and are handled as
regular object.Those are the only differences to JSON.stringify()
. This is a side effect free
variant and [toJSON
][], [replacer
][] and the [spacer
][] work the same as
with JSON.stringify()
.
Currently this is by far the fastest known stable (deterministic) stringify implementation. This is especially important for big objects and TypedArrays.
(Dell Precision 5540, i7-9850H CPU @ 2.60GHz, Node.js 16.11.1)
1simple: simple object x 3,463,894 ops/sec ±0.44% (98 runs sampled) 2simple: circular x 1,236,007 ops/sec ±0.46% (99 runs sampled) 3simple: deep x 18,942 ops/sec ±0.41% (93 runs sampled) 4simple: deep circular x 18,690 ops/sec ±0.72% (96 runs sampled) 5 6replacer: simple object x 2,664,940 ops/sec ±0.31% (98 runs sampled) 7replacer: circular x 1,015,981 ops/sec ±0.09% (99 runs sampled) 8replacer: deep x 17,328 ops/sec ±0.38% (97 runs sampled) 9replacer: deep circular x 17,071 ops/sec ±0.21% (98 runs sampled) 10 11array: simple object x 3,869,608 ops/sec ±0.22% (98 runs sampled) 12array: circular x 3,853,943 ops/sec ±0.45% (96 runs sampled) 13array: deep x 3,563,227 ops/sec ±0.20% (100 runs sampled) 14array: deep circular x 3,286,475 ops/sec ±0.07% (100 runs sampled) 15 16indentation: simple object x 2,183,162 ops/sec ±0.66% (97 runs sampled) 17indentation: circular x 872,538 ops/sec ±0.57% (98 runs sampled) 18indentation: deep x 16,795 ops/sec ±0.48% (93 runs sampled) 19indentation: deep circular x 16,443 ops/sec ±0.40% (97 runs sampled)
Comparing safe-stable-stringify
with known alternatives:
1fast-json-stable-stringify x 18,765 ops/sec ±0.71% (94 runs sampled) 2json-stable-stringify x 13,870 ops/sec ±0.72% (94 runs sampled) 3fast-stable-stringify x 21,343 ops/sec ±0.33% (95 runs sampled) 4faster-stable-stringify x 17,707 ops/sec ±0.44% (97 runs sampled) 5json-stringify-deterministic x 11,208 ops/sec ±0.57% (98 runs sampled) 6fast-safe-stringify x 21,460 ops/sec ±0.75% (99 runs sampled) 7this x 30,367 ops/sec ±0.39% (96 runs sampled) 8 9The fastest is this
The fast-safe-stringify
comparison uses the modules stable implementation.
No vulnerabilities found.
No security vulnerabilities found.