Safely and quickly serialize JavaScript objects
Hello! Fixes #44 In short, this fixes an issue where circular references in getters weren't replaced in `decirc` which threw the error`TypeError: Converting circular structure to JSON` when eventually passed to `JSON.stringify`. To not have that problem, I remove the property and set it to `'[Circular]'` since straight up setting it won't work (unless it has a defined setter). Example object that threw before: ```javascript const o = { a: 1, get self() { return o }, } ``` Let me know if there's anything else I can do to try to remediate this. :) I ran the benchmarks which yielded the below results. I wouldn't trust them fully though as I'd have expected my changes to have been objectively slower on all cases? Results seems to be close enough though, I believe? **Before**: ``` util.inspect: simple object x 43,740 ops/sec ±0.45% (88 runs sampled) util.inspect: circular x 21,473 ops/sec ±0.64% (92 runs sampled) util.inspect: deep x 3,067 ops/sec ±0.77% (93 runs sampled) util.inspect: deep circular x 3,054 ops/sec ±0.59% (92 runs sampled) json-stringify-safe: simple object x 335,428 ops/sec ±1.22% (92 runs sampled) json-stringify-safe: circular x 148,669 ops/sec ±0.74% (90 runs sampled) json-stringify-safe: deep x 8,256 ops/sec ±1.04% (91 runs sampled) json-stringify-safe: deep circular x 8,160 ops/sec ±0.66% (90 runs sampled) fast-safe-stringify: simple object x 1,140,411 ops/sec ±0.62% (92 runs sampled) fast-safe-stringify: circular x 604,163 ops/sec ±0.67% (93 runs sampled) fast-safe-stringify: deep x 38,762 ops/sec ±0.88% (91 runs sampled) fast-safe-stringify: deep circular x 38,023 ops/sec ±0.64% (94 runs sampled) Fastest is fast-safe-stringify: simple object ``` --- **After**: ``` util.inspect: simple object x 45,068 ops/sec ±0.53% (86 runs sampled) util.inspect: circular x 22,214 ops/sec ±0.82% (88 runs sampled) util.inspect: deep x 3,239 ops/sec ±0.70% (93 runs sampled) util.inspect: deep circular x 3,191 ops/sec ±0.93% (92 runs sampled) json-stringify-safe: simple object x 347,556 ops/sec ±0.70% (92 runs sampled) json-stringify-safe: circular x 154,494 ops/sec ±0.68% (91 runs sampled) json-stringify-safe: deep x 8,541 ops/sec ±0.64% (94 runs sampled) json-stringify-safe: deep circular x 8,342 ops/sec ±0.76% (91 runs sampled) fast-safe-stringify: simple object x 1,144,744 ops/sec ±0.51% (92 runs sampled) fast-safe-stringify: circular x 557,971 ops/sec ±0.97% (93 runs sampled) fast-safe-stringify: deep x 38,949 ops/sec ±0.60% (92 runs sampled) fast-safe-stringify: deep circular x 37,684 ops/sec ±0.65% (92 runs sampled) Fastest is fast-safe-stringify: simple object ```
This issue appears to be discussing a feature request or bug report related to the repository. Based on the content, it seems to be resolved. The issue was opened by kristofferremback and has received 9 comments.