fast-safe-stringify
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -6,8 +6,14 @@ var bench = require('fastbench') | ||
var obj = {foo: 1} | ||
obj.o = {obj: obj} | ||
var circ = JSON.parse(JSON.stringify(obj)) | ||
circ.o = {obj: circ} | ||
var deep = require('./package.json') | ||
deep.deep = Object.assign({}, deep) | ||
deep.deep.deep = Object.assign({}, deep.deep) | ||
deep.deep.deep.deep = Object.assign({}, deep.deep.deep) | ||
deep.deep = JSON.parse(JSON.stringify(deep)) | ||
deep.deep.deep = JSON.parse(JSON.stringify(deep)) | ||
deep.deep.deep.deep = JSON.parse(JSON.stringify(deep)) | ||
var deepCirc = JSON.parse(JSON.stringify(deep)) | ||
deepCirc.deep.deep.deep.circ = deepCirc | ||
deepCirc.deep.deep.circ = deepCirc | ||
deepCirc.deep.circ = deepCirc | ||
var run = bench([ | ||
@@ -25,3 +31,15 @@ function inspectBench (cb) { | ||
setImmediate(cb) | ||
}, | ||
function inspectCircBench (cb) { | ||
inspect(circ) | ||
setImmediate(cb) | ||
}, | ||
function jsonStringifyCircSafeBench (cb) { | ||
jsonStringifySafe(circ) | ||
setImmediate(cb) | ||
}, | ||
function fastSafeStringifyCircBench (cb) { | ||
fastSafeStringify(circ) | ||
setImmediate(cb) | ||
}, | ||
function inspectDeepBench (cb) { | ||
@@ -38,5 +56,29 @@ inspect(deep) | ||
setImmediate(cb) | ||
} | ||
}, | ||
function inspectDeepCircBench (cb) { | ||
inspect(deepCirc) | ||
setImmediate(cb) | ||
}, | ||
function jsonStringifySafeDeepCircBench (cb) { | ||
jsonStringifySafe(deepCirc) | ||
setImmediate(cb) | ||
}, | ||
function fastSafeStringifyDeepCircBench (cb) { | ||
fastSafeStringify(deepCirc) | ||
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) |
{ | ||
"name": "fast-safe-stringify", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Safely and quickly serialize JavaScript objects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,18 +28,37 @@ # fast-safe-stringify | ||
```js | ||
inspectBench*10000: 132.456ms | ||
jsonStringifySafeBench*10000: 67.382ms | ||
fastSafeStringifyBench*10000: 31.672ms | ||
inspectBench*10000: 179.608ms | ||
jsonStringifySafeBench*10000: 30.099ms | ||
fastSafeStringifyBench*10000: 19.965ms | ||
inspectDeepBench*10000: 1632.687ms | ||
jsonStringifySafeDeepBench*10000: 1062.449ms | ||
fastSafeStringifyDeepBench*10000: 177.926ms | ||
inspectCircBench*10000: 220.763ms | ||
jsonStringifyCircSafeBench*10000: 39.115ms | ||
fastSafeStringifyCircBench*10000: 29.444ms | ||
inspectDeepBench*10000: 2231.148ms | ||
jsonStringifySafeDeepBench*10000: 880.177ms | ||
fastSafeStringifyDeepBench*10000: 230.209ms | ||
inspectDeepCircBench*10000: 2221.848ms | ||
jsonStringifySafeDeepCircBench*10000: 922.314ms | ||
fastSafeStringifyDeepCircBench*10000: 236.024ms | ||
``` | ||
`fast-safe-stringify` is 2x faster for small objects, | ||
and 6x faster for large objects than `json-stringify-safe`. | ||
## Protip | ||
`fast-safe-stringify` is 4x faster for small objects, | ||
and 9x faster for large objects than `util.inspect`. | ||
Whether you're using `fast-safe-stringify` or `json-stringify-safe` | ||
if you're use case consists of deeply nested objects without circular | ||
references the following pattern will give you best results: | ||
```js | ||
var fastSafeStringify = require('fast-safe-stringify') | ||
function tryStringify (obj) { | ||
try { return JSON.stringify(obj) } catch (_) {} | ||
} | ||
var str = tryStringify(deep) || fastSafeStringify(deep) | ||
``` | ||
If you're likely do be handling mostly shallow or one level nested objects, | ||
this same pattern will degrade performance - it's entirely dependant on use case. | ||
## Acknowledgements | ||
@@ -46,0 +65,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10993
265
71