Gathering detailed insights and metrics for safe-stable-stringify
Gathering detailed insights and metrics for safe-stable-stringify
Gathering detailed insights and metrics for safe-stable-stringify
Gathering detailed insights and metrics for safe-stable-stringify
fast-safe-stringify
Safely and quickly serialize JavaScript objects
json-stable-stringify
deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results
json-stable-stringify-without-jsonify
deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results, with no public domain dependencies
fast-json-stable-stringify
deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify
Safe, deterministic and fast serialization alternative to JSON.stringify.
npm install safe-stable-stringify
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
204 Stars
91 Commits
16 Forks
5 Watching
1 Branches
11 Contributors
Updated on 24 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.3%
3,048,518
Compared to previous day
Last week
2.6%
16,259,760
Compared to previous week
Last month
10.7%
67,200,110
Compared to previous month
Last year
35.9%
671,522,817
Compared to previous year
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|function} If true
or a Array#sort(comparator)
comparator method, 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 as well as Symbol keys!
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.
Sponsored by MaibornWolff and nearForm
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 5/29 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More