Package Exports
- fast-stringify
- fast-stringify/dist/index.cjs.js
- fast-stringify/dist/index.esm.js
- fast-stringify/dist/index.js
- fast-stringify/mjs/index.mjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fast-stringify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
fast-stringify
A tiny, blazing fast stringifier that safely handles circular objects.
The fastest way to stringify an object will always be the native JSON.stringify, but it does not support circular objects out of the box. If you need to stringify objects that have circular references, fast-stringify is there for you! It hsa a simple API to allow for several use-cases that JSON.stringify does not, and aims to be the most performant stringifier that handles circular references.
Table of contents
Usage
import stringify from "fast-stringify";
const object = {
foo: "bar",
deeply: {
recursive: {
object: {},
},
},
};
object.deeply.recursive.object = object.deeply.recursive;
console.log(stringify(object));
// {"foo":"bar","deeply":{"recursive":{"object":"[ref=.deeply.recursive]"}}}stringify
interface Options {
circularReplacer?: (key: string, value: any, referenceKey: string) => any;
indent?: number;
replacer?: (key: string, value: any) => any;
stable?: boolean;
stabilizer?: (
entryA: { key: string; value: any },
entryB: { key: string; value: any },
stabilizerOptions: { get: (key: string) => any }
) => any;
}
function stringify(value: any, options?: Options): string;Stringifies the object passed based on the options passed. The only required value is the value. The additional optons passed will customize how the string is compiled. Available options:
replacer=> function to customize how the non-circular value is stringified (see the documentation for JSON.stringify for more details)indent=> number of spaces to indent the stringified object for pretty-printing (see the documentation for JSON.stringify for more details)circularReplacer=> function to customize how the circular value is stringified (defaults to[ref=##]where##is thereferenceKey)referenceKeyis a dot-separated key list reflecting the nested key the object was originally declared at
stable=> whether to sort the keys for stabilitystabilizer=> function to customize how the stable object is sorted (only applies whenstableistrue)
Importing
// ESM in browsers
import stringify from "fast-stringify";
// ESM in NodeJS
import stringify from "fast-stringify/mjs";
// CommonJS
const stringify = require("fast-stringify");Benchmarks
Simple objects
fast-stringify x 1,104,098 ops/sec ±0.65% (90 runs sampled) fast-json-stable-stringify x 996,823 ops/sec ±0.25% (99 runs sampled) json-stringify-safe x 746,892 ops/sec ±0.30% (100 runs sampled) json-stable-stringify x 707,415 ops/sec ±0.24% (99 runs sampled) decircularize x 376,122 ops/sec ±0.18% (95 runs sampled) json-cycle x 6,479 ops/sec ±0.16% (91 runs sampled)
Complex objects
fast-stringify x 197,784 ops/sec ±0.21% (97 runs sampled) fast-json-stable-stringify x 197,368 ops/sec ±0.15% (97 runs sampled) json-stringify-safe x 141,750 ops/sec ±0.17% (100 runs sampled) json-stable-stringify x 117,201 ops/sec ±0.23% (94 runs sampled) decircularize x 61,345 ops/sec ±0.17% (97 runs sampled) json-cycle x 1,059 ops/sec ±0.15% (88 runs sampled)
Circular objects
fast-stringify x 174,982 ops/sec ±0.14% (101 runs sampled) json-stringify-safe x 122,812 ops/sec ±0.14% (98 runs sampled) decircularize x 54,176 ops/sec ±0.25% (96 runs sampled) json-cycle x 987 ops/sec ±0.28% (86 runs sampled) fast-json-stable-stringify: ERROR json-stable-stringify: ERROR
Special objects
fast-stringify x 72,741 ops/sec ±0.22% (98 runs sampled) fast-json-stable-stringify x 54,683 ops/sec ±0.15% (101 runs sampled) json-stringify-safe x 55,118 ops/sec ±0.31% (97 runs sampled) json-stable-stringify x 36,669 ops/sec ±0.14% (101 runs sampled) decircularize x 21,645 ops/sec ±0.27% (100 runs sampled) json-cycle x 376 ops/sec ±0.18% (82 runs sampled)
Stable objects
json-fast-stable-stringify x 818,266 ops/sec ±0.66% (95 runs sampled) fast-stringify x 579,210 ops/sec ±0.32% (95 runs sampled) json-stable-stringify x 478,709 ops/sec ±0.29% (99 runs sampled)
Development
Standard practice, clone the repo and npm i to get the dependencies. The following npm scripts are available:
benchmark=> run benchmark tests against other equality librariesbuild=> build dist files withrollupclean=> runclean:distandclean:mjsscriptsclean:dist=> runrimrafon thedistfolderclean:mjs=> runrimrafon themjsfoldercopy:mjs=> copy and transform the ESM file generated bydistto be consumable as an.mjsfiledev=> start webpack playground Appdist=> runclean,build, andcopy:mjsscriptslint=> run ESLint on all files insrcfolder (also runs ondevscript)lint:fix=> runlintscript, but with auto-fixerrelease=> runrelease-itfor standard versions (expected to be installed globally)release:beta=> runrelease-itfor beta versions (expected to be installed globally)release:scripts=> runlint,typecheck,test:coverage, anddistscriptsstart=> rundevtest=> run Jest with NODE_ENV=test on all files in__tests__foldertest:coverage=> run same script astestwith code coverage calculationtest:watch=> run same script astestbut keep persistent watchertypecheck=> run TypeScript types validation