npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-superstring

v2.1.0

Published

Fast, safe JSON stringification.

Downloads

10

Readme

json-superstring

The native JSON.stringify() method is quite fast, but it lacks safety checks for things like circular references and enumerable getter properties that throw errors. The json-superstring module wraps JSON.stringify(), and includes safety checks for these potential issues.

Table of Contents

Usage

Add json-superstring as a dependency in package.json:

$ npm install json-superstring -S

Then simply call json-superstring...

const jsonSuperstring = require('json-superstring');

const data = {
  foo: {
    a: '1',
    b: 2
  }
};
data.foo.bar = data.foo;

console.log(jsonSuperstring(data));
// {"foo":{"a":"1","b":2,"bar":"[Circular]"}}

API

jsonSuperstring(data [, space])

Stringifies a value in the same manner as JSON.stringify(). The key difference is circular references and getter properties that throw errors do not prevent stringification.

Parameters

  • data: (required) the data to stringify.

  • space: (optional) a String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.

Returns

A JSON string.

jsonSuperstring.merge(...data)

Safely merges n-number of objects together. The resulting object can be safely used with JSON.stringify().

Parameters

  • ...data: (required) n-number of objects to merge.

Returns

An object.

Motivation

There are a number of other safe JSON stringifiers out there, and they all provide some variation of performance and features. The goals of this project are:

  1. Best in class performance.

  2. Safety checks for both circular references and getter properties that errors.

  3. No side effects. Objects passed to json-stringify are not modified.

  4. Provide ability to specify space param, consistent with JSON.stringify()'s functionality.

  5. Provide ability to merge multiple objects together and stringify.

Comparison

| Name | White Space | Circular Check | Error Check | No Side Effects | |------------------------|-------------|----------------|-------------|-----------------| | json-superstring | ✔ | ✔ | ✔ | ✔ | | fast-safe-stringify | ✕ | ✔ | ✕ | ✕ | | json-stringify-safe | ✔ | ✔[] | ✕ | ✔ | | safe-json-stringify | ✕ | ✔[][**] | ✔ | ✕ |

[*] Does not check for circular references when calling an object's toJSON() method.

[**] While safe-json-stringify performs circular reference checks, it marks all duplicate object references in a JSON object as [Circular] regardless of whether they are actual circular references.

Performance

Performance metrics should always be taken with a healthy level of skepticism. Different software performs differently as the parameters change, and as the state of the host environment changes.

Moreover, not all software provides the same functionality. As demonstrated in the Comparison table above, that is the case with json-superstring, and other safe JSON stringifiers.

The goal here is to show that json-superstring's performance is comparable with other libraries while providing a broader set of features.

Benchmark reports are generated using radargun, and built using data from 1 million runs of each utility.

circular.bench.js

┌─────────────────────┬─────────────┬─────────────┬─────────────┐
│ NAME                │ AVG         │ MIN         │ MAX         │
╞═════════════════════╪═════════════╪═════════════╪═════════════╡
│ json-superstring    │ 7582 ns     │ 5666 ns     │ 1794442 ns  │
├─────────────────────┼─────────────┼─────────────┼─────────────┤
│ fast-safe-stringify │ 8810 ns     │ 6216 ns     │ 3692754 ns  │
├─────────────────────┼─────────────┼─────────────┼─────────────┤
│ json-stringify-safe │ 10928 ns    │ 6397 ns     │ 16823572 ns │
├─────────────────────┼─────────────┼─────────────┼─────────────┤
│ safe-json-stringify │ 8231 ns     │ 4597 ns     │ 12536094 ns │
└─────────────────────┴─────────────┴─────────────┴─────────────┘

throws.bench.js

┌─────────────────────┬─────────────┬─────────────┬─────────────┐
│ NAME                │ AVG         │ MIN         │ MAX         │
╞═════════════════════╪═════════════╪═════════════╪═════════════╡
│ json-superstring    │ 9626 ns     │ 6157 ns     │ 7036140 ns  │
├─────────────────────┼─────────────┼─────────────┼─────────────┤
│ safe-json-stringify │ 13183 ns    │ 8427 ns     │ 12135604 ns │
└─────────────────────┴─────────────┴─────────────┴─────────────┘

merge.bench.js

┌──────────────────┬────────────┬────────────┬────────────┐
│ NAME             │ AVG        │ MIN        │ MAX        │
╞══════════════════╪════════════╪════════════╪════════════╡
│ json-superstring │ 9443 ns    │ 6137 ns    │ 5428283 ns │
└──────────────────┴────────────┴────────────┴────────────┘