Socket
Socket
Sign inDemoInstall

fast-safe-stringify

Package Overview
Dependencies
0
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.1 to 1.2.2

.npmignore

12

benchmark.js

@@ -66,17 +66,5 @@ var bench = require('fastbench')

setImmediate(cb)
},
function jsonStringifyDeepTryFirstBench (cb) {
tryStringify(deep) || jsonStringifySafe(deep)
setImmediate(cb)
},
function fastSafeStringifyDeepTryFirstBench (cb) {
tryStringify(deep) || fastSafeStringify(deep)
setImmediate(cb)
}
], 10000)
function tryStringify (obj) {
try { return JSON.stringify(obj) } catch (_) {}
}
run(run)

56

index.js
module.exports = stringify
stringify.default = stringify
function stringify (obj) {
if (obj !== null && typeof obj === 'object' && typeof obj.toJSON !== 'function') {
decirc(obj, '', [], null)
}
decirc(obj, '', [], null)
return JSON.stringify(obj)

@@ -22,38 +20,26 @@ }

function decirc (val, k, stack, parent) {
var keys, len, i, j, exists, stackLen
if (typeof val !== 'object' || val === null) {
// not an object, nothing to do
return
} else if (val instanceof Circle) {
val.count++
return
} else if (typeof val.toJSON === 'function' && !val.toJSON.forceDecirc) {
return
} else if (parent) {
j = 0
exists = false
stackLen = stack.length
for (; j < stackLen; j++) {
if (stack[j] === val) {
exists = true
break
if (typeof val === 'object' && val !== null) {
if (typeof val.toJSON === 'function') {
if (val instanceof Circle) {
val.count++
return
}
if (val.toJSON.forceDecirc === undefined) {
return
}
}
if (exists) {
parent[k] = new Circle(val, k, parent)
return
for (var i = 0; i < stack.length; i++) {
if (stack[i] === val) {
parent[k] = new Circle(val, k, parent)
return
}
}
stack.push(val)
var keys = Object.keys(val)
for (var j = 0; j < keys.length; j++) {
var key = keys[j]
decirc(val[key], key, stack, val)
}
stack.pop()
}
stack.push(val)
keys = []
for (var key in val) {
if (Object.prototype.hasOwnProperty.call(val, key)) keys.push(key)
}
len = keys.length
i = 0
for (; i < len; i++) {
k = keys[i]
decirc(val[k], k, stack, val)
}
stack.pop()
}
{
"name": "fast-safe-stringify",
"version": "1.2.1",
"version": "1.2.2",
"description": "Safely and quickly serialize JavaScript objects",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,4 +5,4 @@ # fast-safe-stringify

Detects circular dependencies instead of throwing
(as per usual `JSON.stringify` usage)
Detects circular dependencies instead of throwing (as per usual `JSON.stringify`
usage)

@@ -13,3 +13,3 @@ ## Usage

var safeStringify = require('fast-safe-stringify')
var o = {a: 1}
var o = { a: 1 }
o.o = o

@@ -23,6 +23,5 @@

`fast-safe-stringify` would not attempt to detect circular dependencies
on objects that have a `toJSON` function. If you need to do that, you
will need to attach a `toJSON.forceDecirc = true` property, like
so:
`fast-safe-stringify` would not attempt to detect circular dependencies on
objects that have a `toJSON` function. If you need to do that, you will need to
attach a `toJSON.forceDecirc = true` property, like so:

@@ -41,5 +40,7 @@ ```js

The [json-stringify-safe](http://npm.im/json-stringify-safe) module supplies similar functionality with more info and flexibility.
The [json-stringify-safe](http://npm.im/json-stringify-safe) module supplies
similar functionality with more info and flexibility.
Although not JSON, the core `util.inspect` method can be used for similar purposes (e.g. logging) and also handles circular references.
Although not JSON, the core `util.inspect` method can be used for similar
purposes (e.g. logging) and also handles circular references.

@@ -49,17 +50,17 @@ Here we compare `fast-safe-stringify` with these alternatives:

```
inspectBench*10000: 179.608ms
jsonStringifySafeBench*10000: 30.099ms
fastSafeStringifyBench*10000: 19.965ms
inspectBench*10000: 44.441ms
jsonStringifySafeBench*10000: 38.324ms
fastSafeStringifyBench*10000: 25.165ms
inspectCircBench*10000: 220.763ms
jsonStringifyCircSafeBench*10000: 39.115ms
fastSafeStringifyCircBench*10000: 29.444ms
inspectCircBench*10000: 66.541ms
jsonStringifyCircSafeBench*10000: 37.949ms
fastSafeStringifyCircBench*10000: 33.801ms
inspectDeepBench*10000: 2231.148ms
jsonStringifySafeDeepBench*10000: 880.177ms
fastSafeStringifyDeepBench*10000: 230.209ms
inspectDeepBench*10000: 377.053ms
jsonStringifySafeDeepBench*10000: 658.650ms
fastSafeStringifyDeepBench*10000: 268.092ms
inspectDeepCircBench*10000: 2221.848ms
jsonStringifySafeDeepCircBench*10000: 922.314ms
fastSafeStringifyDeepCircBench*10000: 236.024ms
inspectDeepCircBench*10000: 351.387ms
jsonStringifySafeDeepCircBench*10000: 695.964ms
fastSafeStringifyDeepCircBench*10000: 256.660ms
```

@@ -69,5 +70,5 @@

Whether you're using `fast-safe-stringify` or `json-stringify-safe`
if your use case consists of deeply nested objects without circular
references the following pattern will give you best results:
Whether you're using `fast-safe-stringify` or `json-stringify-safe` if your use
case consists of deeply nested objects without circular references the following
pattern will give you best results:

@@ -82,4 +83,4 @@ ```js

If you're likely to be handling mostly shallow or one level nested objects,
this same pattern will degrade performance - it's entirely dependant on use case.
If you're likely to be handling mostly shallow or one level nested objects, this
same pattern will degrade performance - it's entirely dependant on use case.

@@ -89,5 +90,5 @@ ## JSON.stringify options

JSON.stringify's `replacer` and `space` options are not supported. Any value
other than 0 for `space` halves the speed, and providing a replacer function
can result in a segfault. Given that the primary focus of this serializer is
speed, the trade offs for supporting these options are not desirable.
other than 0 for `space` halves the speed, and providing a replacer function can
result in a segfault. Given that the primary focus of this serializer is speed,
the trade offs for supporting these options are not desirable.

@@ -94,0 +95,0 @@ ## Acknowledgements

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc