🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis β†’
Socket
Book a DemoInstallSign in
Socket

fast-stringify

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-stringify

A blazing fast stringifier that safely handles circular objects

latest
Source
npmnpm
Version
4.0.0
Version published
Weekly downloads
47K
4.08%
Maintainers
1
Weekly downloads
Β 
Created
Source

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 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

// ESM
import { stringify } from 'fast-stringify';

// CommonJS
const { stringify } = require('fast-stringify');

Benchmarks

Simple objects

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)                    β”‚ Ops/sec β”‚ Margin of error β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ fast-stringify             β”‚ 1246981 β”‚ 'Β± 0.01%'       β”‚
β”‚ faster-stable-stringify    β”‚ 1009331 β”‚ 'Β± 0.02%'       β”‚
β”‚ fast-json-stable-stringify β”‚ 918643  β”‚ 'Β± 0.02%'       β”‚
β”‚ json-stringify-safe        β”‚ 736089  β”‚ 'Β± 0.02%'       β”‚
β”‚ json-stable-stringify      β”‚ 655688  β”‚ 'Β± 0.02%'       β”‚
β”‚ decircularize              β”‚ 434227  β”‚ 'Β± 0.03%'       β”‚
β”‚ superjson                  β”‚ 260614  β”‚ 'Β± 0.03%'       β”‚
β”‚ json-cycle                 β”‚ 5930    β”‚ 'Β± 0.06%'       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Fastest was "fast-stringify".

Complex objects

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)                    β”‚ Ops/sec β”‚ Margin of error β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ fast-stringify             β”‚ 182435  β”‚ 'Β± 0.04%'       β”‚
β”‚ fast-json-stable-stringify β”‚ 181922  β”‚ 'Β± 0.03%'       β”‚
β”‚ faster-stable-stringify    β”‚ 164295  β”‚ 'Β± 0.03%'       β”‚
β”‚ json-stringify-safe        β”‚ 133740  β”‚ 'Β± 0.04%'       β”‚
β”‚ json-stable-stringify      β”‚ 110923  β”‚ 'Β± 0.04%'       β”‚
β”‚ decircularize              β”‚ 60097   β”‚ 'Β± 0.05%'       β”‚
β”‚ superjson                  β”‚ 41319   β”‚ 'Β± 0.06%'       β”‚
β”‚ json-cycle                 β”‚ 959     β”‚ 'Β± 0.06%'       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Fastest was "fast-stringify".

Circular objects

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)                    β”‚ Ops/sec β”‚ Margin of error β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ fast-stringify             β”‚ 154881  β”‚ 'Β± 0.04%'       β”‚
β”‚ fast-json-stable-stringify β”‚ 148094  β”‚ 'Β± 0.04%'       β”‚
β”‚ faster-stable-stringify    β”‚ 139589  β”‚ 'Β± 0.04%'       β”‚
β”‚ json-stringify-safe        β”‚ 115500  β”‚ 'Β± 0.05%'       β”‚
β”‚ json-stable-stringify      β”‚ 95238   β”‚ 'Β± 0.06%'       β”‚
β”‚ decircularize              β”‚ 54071   β”‚ 'Β± 0.08%'       β”‚
β”‚ superjson                  β”‚ 32388   β”‚ 'Β± 0.08%'       β”‚
β”‚ json-cycle                 β”‚ 883     β”‚ 'Β± 0.12%'       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Fastest was "fast-stringify".

Special objects

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)                    β”‚ Ops/sec β”‚ Margin of error β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ fast-stringify             β”‚ 68639   β”‚ 'Β± 0.06%'       β”‚
β”‚ json-stringify-safe        β”‚ 52087   β”‚ 'Β± 0.06%'       β”‚
β”‚ fast-json-stable-stringify β”‚ 51025   β”‚ 'Β± 0.06%'       β”‚
β”‚ faster-stable-stringify    β”‚ 46096   β”‚ 'Β± 0.07%'       β”‚
β”‚ json-stable-stringify      β”‚ 33479   β”‚ 'Β± 0.08%'       β”‚
β”‚ decircularize              β”‚ 20333   β”‚ 'Β± 0.11%'       β”‚
β”‚ superjson                  β”‚ 14914   β”‚ 'Β± 0.10%'       β”‚
β”‚ json-cycle                 β”‚ 341     β”‚ 'Β± 0.11%'       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Fastest was "fast-stringify".

Stable objects

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)                    β”‚ Ops/sec β”‚ Margin of error β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ fast-json-stable-stringify β”‚ 673986  β”‚ 'Β± 0.02%'       β”‚
β”‚ faster-stable-stringify    β”‚ 645572  β”‚ 'Β± 0.03%'       β”‚
β”‚ fast-stringify             β”‚ 524383  β”‚ 'Β± 0.02%'       β”‚
β”‚ json-stable-stringify      β”‚ 398551  β”‚ 'Β± 0.03%'       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Fastest was "fast-json-stable-stringify".

Stable circular objects

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)                    β”‚ Ops/sec β”‚ Margin of error β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ fast-json-stable-stringify β”‚ 479587  β”‚ 'Β± 0.03%'       β”‚
β”‚ faster-stable-stringify    β”‚ 477015  β”‚ 'Β± 0.03%'       β”‚
β”‚ fast-stringify             β”‚ 380990  β”‚ 'Β± 0.03%'       β”‚
β”‚ json-stable-stringify      β”‚ 288945  β”‚ 'Β± 0.03%'       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Fastest was "fast-json-stable-stringify".

Keywords

stringify

FAQs

Package last updated on 11 Nov 2025

Did you know?

Socket

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.

Install

Related posts