Package Exports
- fast-stringify
- fast-stringify/dist/index.cjs.js
- fast-stringify/dist/index.esm.js
- fast-stringify/dist/index.js
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 while also maintaining blazing fast performance compared to its peers.
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
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 1129716 │ '± 0.01%' │
│ fast-json-stable-stringify │ 897600 │ '± 0.02%' │
│ json-stringify-safe │ 714530 │ '± 0.02%' │
│ json-stable-stringify │ 666011 │ '± 0.02%' │
│ decircularize │ 374814 │ '± 0.02%' │
│ superjson │ 296722 │ '± 0.02%' │
│ json-cycle │ 6612 │ '± 0.07%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".Complex objects
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 205011 │ '± 0.02%' │
│ fast-json-stable-stringify │ 193249 │ '± 0.03%' │
│ json-stringify-safe │ 145528 │ '± 0.03%' │
│ json-stable-stringify │ 116081 │ '± 0.03%' │
│ decircularize │ 60842 │ '± 0.04%' │
│ superjson │ 47221 │ '± 0.06%' │
│ json-cycle │ 1113 │ '± 0.11%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".Circular objects
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 165949 │ '± 0.03%' │
│ fast-json-stable-stringify │ 164775 │ '± 0.03%' │
│ json-stringify-safe │ 124976 │ '± 0.03%' │
│ json-stable-stringify │ 101248 │ '± 0.04%' │
│ decircularize │ 54992 │ '± 0.05%' │
│ superjson │ 37815 │ '± 0.07%' │
│ json-cycle │ 1040 │ '± 0.15%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".Special objects
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 81044 │ '± 0.04%' │
│ json-stringify-safe │ 58829 │ '± 0.05%' │
│ fast-json-stable-stringify │ 55647 │ '± 0.05%' │
│ json-stable-stringify │ 36610 │ '± 0.07%' │
│ decircularize │ 21891 │ '± 0.09%' │
│ superjson │ 16050 │ '± 0.13%' │
│ json-cycle │ 401 │ '± 0.07%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".Stable objects
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-json-stable-stringify │ 657770 │ '± 0.02%' │
│ fast-stringify │ 554002 │ '± 0.02%' │
│ json-stable-stringify │ 426408 │ '± 0.02%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-json-stable-stringify".Stable circular objects
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-json-stable-stringify │ 487884 │ '± 0.02%' │
│ fast-stringify │ 395479 │ '± 0.02%' │
│ json-stable-stringify │ 317215 │ '± 0.02%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-json-stable-stringify".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