fast-safe-stringify

Fast JSON.stringify drop-in that safely handles circular references

Library
npm
v2.1.1
359stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
41/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
69/100Good
Architecture65
Code Quality68
Innovation55
Learning Curve88

fast-safe-stringify is a minimal, dependency-free drop-in replacement for JSON.stringify that detects and safely replaces circular references instead of throwing. It exposes the same (obj, replacer, spacer) signature as the native JSON.stringify, so it can be swapped in without changing call sites, and adds an optional stable/stableStringify export that produces deterministic, key-sorted output for cases like content hashing or snapshot testing.

The implementation walks the object graph once to detect cycles and depth/edge limits before delegating the actual serialization to the native JSON.stringify, keeping performance close to the built-in while avoiding the TypeError: Converting circular structure to JSON failure mode. It has no runtime dependencies and ships a single ~230-line index.js file plus TypeScript typings.

What You Get

  • A drop-in stringify(obj, replacer, spacer, options) function matching the native JSON.stringify signature
  • Automatic circular-reference detection, replacing cycles with a [Circular] marker instead of throwing
  • Optional depthLimit and edgesLimit options to cap traversal depth/breadth on very large or deep graphs
  • A stringify.stable / stringify.stableStringify variant producing deterministic, key-sorted JSON output
  • Zero runtime dependencies and bundled TypeScript type definitions (index.d.ts)

Common Use Cases

  • Structured logging libraries that must serialize arbitrary application objects (including ones with circular references) without crashing
  • Error reporting pipelines that stringify exception objects or request contexts of unknown shape
  • Content hashing or cache-key generation where deterministic (stable) key ordering is required
  • Debug/inspection tooling that dumps live in-memory objects to JSON for display or storage

Under The Hood

Architecture - The library is a single module (index.js) built around two mutually-recursive traversal functions: decirc for the default path and deterministicDecirc for the stable path. Both walk the object graph depth-first using an explicit stack array to detect cycles (rather than a WeakSet, for broader engine compatibility), and both temporarily rewrite offending properties in place — recording every mutation in a module-level arr array — so the original object can be fully restored in a finally block after JSON.stringify runs. setReplace additionally special-cases non-configurable getters by queuing them in a replacerStack that a wrapped replacer function (replaceGetterValues) consults during the real JSON.stringify call.

Tech Stack - Plain ES5-style JavaScript (var, no transpilation step) with a bundled index.d.ts for TypeScript consumers; zero runtime dependencies. Tests use tap, linting uses standard, and a benchmark.js script using the benchmark package compares performance against alternatives like json-stringify-safe.

Code Quality - Two test files (test.js, test-stable.js) run under tap and cover both the default and stable stringify paths, including circular-reference and getter-property cases. The temporary-mutation-then-restore approach is a deliberate trade-off for speed (avoiding cloning) but means the function is not safely reentrant/concurrent on the exact same object graph — an implicit constraint not documented in code comments, worth knowing before using it in highly concurrent contexts.

API Design - The default export mirrors JSON.stringify’s exact parameter order, making adoption a one-line import swap for existing logging code. The stable/stableStringify aliases on the same function object keep the two serialization modes discoverable without a second import, and depthLimit/edgesLimit options are opt-in with sane Number.MAX_SAFE_INTEGER defaults so existing callers see no behavior change until they choose to constrain output.

Used by 6 apps in this directory

TypeScript
72%
AGPL 3.0

APITable

Low Code Platforms · Databases

15,526

API-first collaborative spreadsheet-database platform that auto-generates REST APIs and lets teams build internal tools, CRMs, and dashboards without code.

View details
53
Repo Health
77
Technical
60
Dependency
Built with
TypeScript72%
Java22%
Updated 9 months ago
Python
58%
Apache 2.0

MLflow

AI Development · Monitoring

27,573

The open source AI engineering platform for debugging, evaluating, monitoring, and optimizing production LLMs and agents at scale.

View details
97
Repo Health
86
Technical
68
Dependency
Built with
Python58%
TypeScript33%
Updated today
TypeScript
96%
Other

nango

Developer Tools · Automation · Authentication

11,501

Build product integrations with AI using 800+ APIs — auth, proxy, and TypeScript functions on production-grade infrastructure.

View details
94
Repo Health
85
Technical
69
Dependency
Built with
TypeScript96%
Updated today
TypeScript
27%
AGPL 3.0

Omnivore

Knowledge Management · Bookmarks Archiving · Note Taking

16,221

Self-hosted read-it-later platform with highlights, newsletters, PDFs, and seamless Obsidian and Logseq integration.

View details
78
Repo Health
74
Technical
66
Dependency
Built with
TypeScript27%
JavaScript24%
HTML19%
Updated 2 days ago
TypeScript
97%
MIT

Promptfoo

AI Development

24,362

An open-source CLI and library for evaluating and red-teaming LLM applications — replace trial-and-error prompt engineering with systematic evals, vulnerability scanning, and CI/CD integration.

View details
90
Repo Health
73
Technical
68
Dependency
Built with
TypeScript97%
Updated yesterday
TypeScript
87%
Other

strapi

CMS

72,965

Open-source headless CMS that auto-generates REST and GraphQL APIs from your content models, with a fully customizable admin panel you control.

View details
93
Repo Health
84
Technical
68
Dependency
Built with
TypeScript87%
JavaScript12%
Updated yesterday

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search