Installations
npm install @stoplight/fast-safe-stringify
Developer Guide
Typescript
No
Module System
CommonJS
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
davidmarkclements
Download Statistics
Total Downloads
91,609
Last Day
3
Last Week
8
Last Month
36
Last Year
1,264
GitHub Statistics
349 Stars
153 Commits
27 Forks
9 Watching
1 Branches
16 Contributors
Bundle Size
1.27 kB
Minified
588.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.1.2
Package Id
@stoplight/fast-safe-stringify@2.1.2
Unpacked Size
29.90 kB
Size
6.83 kB
File Count
11
Total Downloads
Cumulative downloads
Total Downloads
91,609
Last day
-40%
3
Compared to previous day
Last week
-61.9%
8
Compared to previous week
Last month
50%
36
Compared to previous month
Last year
-71.3%
1,264
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
5
fast-safe-stringify
Safe and fast serialization alternative to JSON.stringify.
Gracefully handles circular structures instead of throwing.
Provides a deterministic ("stable") version as well that will also gracefully handle circular structures. See the example below for further information.
Usage
The same as JSON.stringify.
stringify(value[, replacer[, space]])
1const safeStringify = require('fast-safe-stringify') 2const o = { a: 1 } 3o.o = o 4 5console.log(safeStringify(o)) 6// '{"a":1,"o":"[Circular]"}' 7console.log(JSON.stringify(o)) 8// TypeError: Converting circular structure to JSON 9 10function replacer(key, value) { 11 console.log('Key:', JSON.stringify(key), 'Value:', JSON.stringify(value)) 12 // Remove the circular structure 13 if (value === '[Circular]') { 14 return 15 } 16 return value 17} 18const serialized = safeStringify(o, replacer, 2) 19// Key: "" Value: {"a":1,"o":"[Circular]"} 20// Key: "a" Value: 1 21// Key: "o" Value: "[Circular]" 22console.log(serialized) 23// { 24// "a": 1 25// }
Using the deterministic version also works the same:
1const safeStringify = require('fast-safe-stringify') 2const o = { b: 1, a: 0 } 3o.o = o 4 5console.log(safeStringify(o)) 6// '{"b":1,"a":0,"o":"[Circular]"}' 7console.log(safeStringify.stableStringify(o)) 8// '{"a":0,"b":1,"o":"[Circular]"}' 9console.log(JSON.stringify(o)) 10// TypeError: Converting circular structure to JSON
A faster and side-effect free implementation is available in the [safe-stable-stringify][] module. However it is still considered experimental due to a new and more complex implementation.
Differences to JSON.stringify
In general the behavior is identical to JSON.stringify. The replacer
and space
options are also available.
A few exceptions exist to JSON.stringify while using toJSON
or
replacer
:
Regular safe stringify
-
Manipulating a circular structure of the passed in value in a
toJSON
or thereplacer
is not possible! It is possible for any other value and property. -
In case a circular structure is detected and the
replacer
is used it will receive the string[Circular]
as the argument instead of the circular object itself.
Deterministic ("stable") safe stringify
-
Manipulating the input object either in a
toJSON
or thereplacer
function will not have any effect on the output. The output entirely relies on the shape the input value had at the point passed to the stringify function! -
In case a circular structure is detected and the
replacer
is used it will receive the string[Circular]
as the argument instead of the circular object itself.
A side effect free variation without these limitations can be found as well
(safe-stable-stringify
). It is also faster than the current
implementation. It is still considered experimental due to a new and more
complex implementation.
Benchmarks
Although not JSON, the Node.js util.inspect
method can be used for similar
purposes (e.g. logging) and also handles circular references.
Here we compare fast-safe-stringify
with some alternatives:
(Lenovo T450s with a i7-5600U CPU using Node.js 8.9.4)
1fast-safe-stringify: simple object x 1,121,497 ops/sec ±0.75% (97 runs sampled) 2fast-safe-stringify: circular x 560,126 ops/sec ±0.64% (96 runs sampled) 3fast-safe-stringify: deep x 32,472 ops/sec ±0.57% (95 runs sampled) 4fast-safe-stringify: deep circular x 32,513 ops/sec ±0.80% (92 runs sampled) 5 6util.inspect: simple object x 272,837 ops/sec ±1.48% (90 runs sampled) 7util.inspect: circular x 116,896 ops/sec ±1.19% (95 runs sampled) 8util.inspect: deep x 19,382 ops/sec ±0.66% (92 runs sampled) 9util.inspect: deep circular x 18,717 ops/sec ±0.63% (96 runs sampled) 10 11json-stringify-safe: simple object x 233,621 ops/sec ±0.97% (94 runs sampled) 12json-stringify-safe: circular x 110,409 ops/sec ±1.85% (95 runs sampled) 13json-stringify-safe: deep x 8,705 ops/sec ±0.87% (96 runs sampled) 14json-stringify-safe: deep circular x 8,336 ops/sec ±2.20% (93 runs sampled)
For stable stringify comparisons, see the performance benchmarks in the
safe-stable-stringify
readme.
Protip
Whether fast-safe-stringify
or alternatives are used: if the use case
consists of deeply nested objects without circular references the following
pattern will give best results.
Shallow or one level nested objects on the other hand will slow down with it.
It is entirely dependant on the use case.
1const stringify = require('fast-safe-stringify') 2 3function tryJSONStringify (obj) { 4 try { return JSON.stringify(obj) } catch (_) {} 5} 6 7const serializedString = tryJSONStringify(deep) || stringify(deep)
Acknowledgements
Sponsored by nearForm
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 7/19 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2025-02-03
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