
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
json-superstring
Advanced tools
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
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]"}}
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.
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:
Best in class performance.
Safety checks for both circular references and getter properties that errors.
No side effects. Objects passed to json-stringify
are not modified.
Provide ability to specify space param, consistent with JSON.stringify()
's functionality.
Provide ability to merge multiple objects together and stringify.
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 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 ā
āāāāāāāāāāāāāāāāāāāā“āāāāāāāāāāāāā“āāāāāāāāāāāāā“āāāāāāāāāāāāā
2.1.0
FAQs
Fast, safe JSON stringification.
We found that json-superstring demonstrated a not healthy version release cadence and project activity because the last version was released 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.