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));
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 the referenceKey)
referenceKey is a dot-separated key list reflecting the nested key the object was originally declared at
stable => whether to sort the keys for stability
stabilizer => function to customize how the stable object is sorted (only applies when stable is true)
Importing
import stringify from "fast-stringify";
import stringify from "fast-stringify/mjs";
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 libraries
build => build dist files with rollup
clean => run clean:dist and clean:mjs scripts
clean:dist => run rimraf on the dist folder
clean:mjs => run rimraf on the mjs folder
copy:mjs => copy and transform the ESM file generated by dist to be consumable as an .mjs file
dev => start webpack playground App
dist => run clean, build, and copy:mjs scripts
lint => run ESLint on all files in src folder (also runs on dev script)
lint:fix => run lint script, but with auto-fixer
release => run release-it for standard versions (expected to be installed globally)
release:beta => run release-it for beta versions (expected to be installed globally)
release:scripts => run lint, typecheck, test:coverage, and dist scripts
start => run dev
test => run Jest with NODE_ENV=test on all files in __tests__ folder
test:coverage => run same script as test with code coverage calculation
test:watch => run same script as test but keep persistent watcher
typecheck => run TypeScript types validation