What is fast-safe-stringify?
The fast-safe-stringify package is designed for safely converting JavaScript objects into JSON strings without running into issues like circular references, which can cause the native JSON.stringify to throw an error. It aims to provide a fast and safe way to serialize objects, including those that may contain circular references, functions, and other non-JSON-safe values.
What are fast-safe-stringify's main functionalities?
Safe serialization of circular references
This feature allows you to safely serialize objects that contain circular references, which would otherwise throw an error with JSON.stringify.
const stringify = require('fast-safe-stringify');
const obj = {};
obj.a = { b: obj };
console.log(stringify(obj));
Stable serialization
Provides an option for stable serialization, where the output JSON string's property order is deterministic, making it suitable for hashing, comparisons, etc.
const stringify = require('fast-safe-stringify').stable;
const obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
console.log(stringify(obj));
Serialization with decycler
Allows custom handling of circular references during serialization, enabling you to replace or transform circular references in the resulting JSON string.
const stringify = require('fast-safe-stringify').stable;
function replaceCircular(key, value, circular) { return circular ? '[Circular]' : value; }
const obj = {};
obj.a = obj;
console.log(stringify(obj, replaceCircular));
Other packages similar to fast-safe-stringify
json-stringify-safe
Similar to fast-safe-stringify, json-stringify-safe provides a way to safely serialize objects into JSON strings, handling circular references by replacing them with a custom string. It's a bit slower compared to fast-safe-stringify but serves a similar purpose.
flatted
Flatted is a package that serializes and deserializes JavaScript objects, including nested and circular references. Unlike fast-safe-stringify, which returns a JSON string, Flatted returns a flattened string representation that can be re-parsed into the original object structure, including circular references.
fast-safe-stringify
Safely and quickly serialize JavaScript objects
Detects circular dependencies instead of throwing
(as per usual JSON.stringify
usage)
Usage
var safeStringify = require('fast-safe-stringify')
var o = {a: 1}
o.o = o
console.log(safeStringify(o))
console.log(JSON.stringify(o))
Benchmarks
The json-stringify-safe module supplies similar
functionality with slightly more info. Although not JSON,
the core util.inspect
method can be used for similar purposes (e.g.
logging) and also handles circular references.
Here we compare fast-safe-stringify
with these alternatives:
inspectBench*10000: 163.506ms
jsonStringifySafeBench*10000: 71.508ms
fastSafeStringifyBench*10000: 35.447ms
inspectBench*10000: 135.528ms
jsonStringifySafeBench*10000: 64.065ms
fastSafeStringifyBench*10000: 33.956ms
fast-safe-stringify
is 2x faster than json-stringify-safe
and 4x
faster than util.inspect
.
Acknowledgements
Sponsored by nearForm
License
MIT