
Security News
The Nightmare Before Deployment
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.
fast-stringify
Advanced tools
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.
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]"}}}
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 atstable => whether to sort the keys for stabilitystabilizer => function to customize how the stable object is sorted (only applies when stable is true)// ESM in browsers
import stringify from "fast-stringify";
// ESM in NodeJS
import stringify from "fast-stringify/mjs";
// CommonJS
const stringify = require("fast-stringify");
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 1039057 │ '± 0.01%' │
│ fast-json-stable-stringify │ 904936 │ '± 0.02%' │
│ json-stringify-safe │ 730877 │ '± 0.02%' │
│ json-stable-stringify │ 679134 │ '± 0.01%' │
│ decircularize │ 378041 │ '± 0.02%' │
│ json-cycle │ 6720 │ '± 0.05%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 202965 │ '± 0.02%' │
│ fast-json-stable-stringify │ 198331 │ '± 0.02%' │
│ json-stringify-safe │ 146527 │ '± 0.02%' │
│ json-stable-stringify │ 116549 │ '± 0.03%' │
│ decircularize │ 62209 │ '± 0.03%' │
│ json-cycle │ 1130 │ '± 0.11%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".
FAILED: fast-json-stable-stringify ("Converting circular structure to JSON")
FAILED: json-stable-stringify ("Converting circular structure to JSON")
┌─────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├─────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 166143 │ '± 0.02%' │
│ json-stringify-safe │ 127393 │ '± 0.02%' │
│ decircularize │ 55939 │ '± 0.05%' │
│ json-cycle │ 1048 │ '± 0.19%' │
└─────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-stringify │ 73237 │ '± 0.04%' │
│ json-stringify-safe │ 58863 │ '± 0.04%' │
│ fast-json-stable-stringify │ 56959 │ '± 0.05%' │
│ json-stable-stringify │ 36927 │ '± 0.07%' │
│ decircularize │ 22377 │ '± 0.08%' │
│ json-cycle │ 403 │ '± 0.16%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-stringify".
┌────────────────────────────┬─────────┬─────────────────┐
│ (index) │ Ops/sec │ Margin of error │
├────────────────────────────┼─────────┼─────────────────┤
│ fast-json-stable-stringify │ 687124 │ '± 0.02%' │
│ fast-stringify │ 552092 │ '± 0.02%' │
│ json-stable-stringify │ 426991 │ '± 0.02%' │
└────────────────────────────┴─────────┴─────────────────┘
Fastest was "fast-json-stable-stringify".
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 with rollupclean => run clean:dist and clean:mjs scriptsclean:dist => run rimraf on the dist folderclean:mjs => run rimraf on the mjs foldercopy:mjs => copy and transform the ESM file generated by dist to be consumable as an .mjs filedev => start webpack playground Appdist => run clean, build, and copy:mjs scriptslint => run ESLint on all files in src folder (also runs on dev script)lint:fix => run lint script, but with auto-fixerrelease => 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 scriptsstart => run devtest => run Jest with NODE_ENV=test on all files in __tests__ foldertest:coverage => run same script as test with code coverage calculationtest:watch => run same script as test but keep persistent watchertypecheck => run TypeScript types validationFAQs
A blazing fast stringifier that safely handles circular objects
The npm package fast-stringify receives a total of 35,193 weekly downloads. As such, fast-stringify popularity was classified as popular.
We found that fast-stringify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.

Research
/Security News
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.