├── .gitignore ├── package.json ├── README.md ├── test.js ├── LICENSE.md ├── index.js └── bench.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "circular-json-es6", 3 | "version": "2.0.2", 4 | "description": "circular JSON.stringify and JSON.parse, for environments with native ES6 Map", 5 | "main": "index.js", 6 | "devDependencies": { 7 | "circular-json": "^0.3.0", 8 | "jsan": "^3.1.2", 9 | "mocha": "^2.4.5" 10 | }, 11 | "scripts": { 12 | "test": "mocha", 13 | "bench": "node bench.js" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/yyx990803/circular-json-es6.git" 18 | }, 19 | "author": "Evan You", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/yyx990803/circular-json-es6/issues" 23 | }, 24 | "homepage": "https://github.com/yyx990803/circular-json-es6#readme" 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # circular-json-es6 2 | 3 | A replacement for `JSON.stringify` and `JSON.parse` that can handle circular references (persists reference structure). 4 | 5 | **This implementation requires environments with native ES6 Map support,** but is decently faster than [circular-json](https://github.com/WebReflection/circular-json) (see benchmark with `npm run bench`). 6 | 7 | ``` js 8 | var CircularJSON = require('circular-json-es6') 9 | 10 | var obj = {} 11 | obj.a = obj 12 | 13 | var clone = CircularJSON.parse(CircularJSON.stringify(obj)) 14 | 15 | clone.a === clone // -> true 16 | ``` 17 | 18 | ### NOTE 19 | 20 | The default `stringify` method optimizes for cases where no circular reference is present by trying a plain `JSON.stringify` first. This means if no circular references are found in the data then it will not persist multiple (but non-circular) references to the same object. 21 | 22 | If you want to enforce reference persistence, use `CircularJSON.stringifyStrict` instead. 23 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var c = require('./index') 2 | var assert = require('assert') 3 | 4 | describe('circular-json', function () { 5 | 6 | var o = {} 7 | o.a = o 8 | o.c = {} 9 | o.d = { 10 | a: 123, 11 | b: o 12 | } 13 | o.c.e = o 14 | o.c.f = o.d 15 | o.b = o.c 16 | o.arr = [o.a, o.c, o.d] 17 | o.d.arr = o.arr 18 | 19 | o = c.parse(c.stringify(o)) 20 | 21 | it('raw value', function () { 22 | assert.ok(o.d.a === 123) 23 | }) 24 | 25 | it('self reference', function () { 26 | assert.ok(o.a === o) 27 | }) 28 | 29 | it('nested self reference', function () { 30 | assert.ok(o.c.e === o) 31 | assert.ok(o.d.b === o) 32 | }) 33 | 34 | it('nested cross reference', function () { 35 | assert.ok(o.c.f === o.d) 36 | assert.ok(o.b = o.c) 37 | assert.ok(o.arr === o.d.arr) 38 | }) 39 | 40 | it('array reference', function () { 41 | assert.ok(o.arr[0] === o.a) 42 | assert.ok(o.arr[1] === o.c) 43 | assert.ok(o.arr[2] === o.d) 44 | }) 45 | }) 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2018 Evan You 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function encode (data, replacer, list, seen) { 2 | var stored, key, value, i, l 3 | var seenIndex = seen.get(data) 4 | if (seenIndex != null) { 5 | return seenIndex 6 | } 7 | var index = list.length 8 | var proto = Object.prototype.toString.call(data) 9 | if (proto === '[object Object]') { 10 | stored = {} 11 | seen.set(data, index) 12 | list.push(stored) 13 | var keys = Object.keys(data) 14 | for (i = 0, l = keys.length; i < l; i++) { 15 | key = keys[i] 16 | value = data[key] 17 | if (replacer) value = replacer.call(data, key, value) 18 | stored[key] = encode(value, replacer, list, seen) 19 | } 20 | } else if (proto === '[object Array]') { 21 | stored = [] 22 | seen.set(data, index) 23 | list.push(stored) 24 | for (i = 0, l = data.length; i < l; i++) { 25 | value = data[i] 26 | if (replacer) value = replacer.call(data, i, value) 27 | stored[i] = encode(value, replacer, list, seen) 28 | } 29 | } else { 30 | list.push(data) 31 | } 32 | return index 33 | } 34 | 35 | function decode (list, reviver) { 36 | var i = list.length 37 | var j, k, data, key, value, proto 38 | while (i--) { 39 | data = list[i] 40 | proto = Object.prototype.toString.call(data) 41 | if (proto === '[object Object]') { 42 | var keys = Object.keys(data) 43 | for (j = 0, k = keys.length; j < k; j++) { 44 | key = keys[j] 45 | value = list[data[key]] 46 | if (reviver) value = reviver.call(data, key, value) 47 | data[key] = value 48 | } 49 | } else if (proto === '[object Array]') { 50 | for (j = 0, k = data.length; j < k; j++) { 51 | value = list[data[j]] 52 | if (reviver) value = reviver.call(data, j, value) 53 | data[j] = value 54 | } 55 | } 56 | } 57 | } 58 | 59 | exports.stringify = function stringify (data, replacer, space) { 60 | try { 61 | return arguments.length === 1 62 | ? JSON.stringify(data) 63 | : JSON.stringify(data, replacer, space) 64 | } catch (e) { 65 | return exports.stringifyStrict(data, replacer, space) 66 | } 67 | } 68 | 69 | exports.parse = function parse (data, reviver) { 70 | var hasCircular = /^\s/.test(data) 71 | if (!hasCircular) { 72 | return arguments.length === 1 73 | ? JSON.parse(data) 74 | : JSON.parse(data, reviver) 75 | } else { 76 | var list = JSON.parse(data) 77 | decode(list, reviver) 78 | return list[0] 79 | } 80 | } 81 | 82 | exports.stringifyStrict = function stringifyStrict (data, replacer, space) { 83 | var list = [] 84 | encode(data, replacer, list, new Map()) 85 | return space 86 | ? ' ' + JSON.stringify(list, null, space) 87 | : ' ' + JSON.stringify(list) 88 | } 89 | -------------------------------------------------------------------------------- /bench.js: -------------------------------------------------------------------------------- 1 | var dummy100 = [{"id":1,"sex":"Female","age":38,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"1st 2nd 3rd or 4th grade","race":"White"},{"id":2,"sex":"Female","age":44,"classOfWorker":"Self-employed-not incorporated","maritalStatus":"Married-civilian spouse present","education":"Associates degree-occup /vocational","race":"White"},{"id":3,"sex":"Male","age":2,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":4,"sex":"Female","age":35,"classOfWorker":"Private","maritalStatus":"Divorced","education":"High school graduate","race":"White"},{"id":5,"sex":"Male","age":49,"classOfWorker":"Private","maritalStatus":"Divorced","education":"High school graduate","race":"White"},{"id":6,"sex":"Male","age":13,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":7,"sex":"Female","age":1,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":8,"sex":"Female","age":61,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":9,"sex":"Male","age":38,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Masters degree(MA MS MEng MEd MSW MBA)","race":"Black"},{"id":10,"sex":"Female","age":7,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":11,"sex":"Female","age":30,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Bachelors degree(BA AB BS)","race":"White"},{"id":12,"sex":"Male","age":85,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"10th grade","race":"White"},{"id":13,"sex":"Female","age":33,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"Asian or Pacific Islander"},{"id":14,"sex":"Male","age":26,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"White"},{"id":15,"sex":"Female","age":46,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":16,"sex":"Female","age":19,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"White"},{"id":17,"sex":"Male","age":11,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":18,"sex":"Male","age":23,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Bachelors degree(BA AB BS)","race":"White"},{"id":19,"sex":"Male","age":27,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":20,"sex":"Male","age":35,"classOfWorker":"Self-employed-not incorporated","maritalStatus":"Divorced","education":"High school graduate","race":"Black"},{"id":21,"sex":"Male","age":8,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":22,"sex":"Female","age":29,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":23,"sex":"Female","age":40,"classOfWorker":"Private","maritalStatus":"Divorced","education":"Some college but no degree","race":"White"},{"id":24,"sex":"Male","age":24,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":25,"sex":"Male","age":45,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Some college but no degree","race":"White"},{"id":26,"sex":"Female","age":27,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"White"},{"id":27,"sex":"Male","age":41,"classOfWorker":"Local government","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":28,"sex":"Female","age":14,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":29,"sex":"Male","age":73,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"7th and 8th grade","race":"White"},{"id":30,"sex":"Male","age":46,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Some college but no degree","race":"White"},{"id":31,"sex":"Female","age":78,"classOfWorker":"Not in universe","maritalStatus":"Widowed","education":"7th and 8th grade","race":"White"},{"id":32,"sex":"Male","age":27,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":33,"sex":"Female","age":81,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"9th grade","race":"White"},{"id":34,"sex":"Male","age":35,"classOfWorker":"Private","maritalStatus":"Never married","education":"Masters degree(MA MS MEng MEd MSW MBA)","race":"White"},{"id":35,"sex":"Female","age":15,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"7th and 8th grade","race":"White"},{"id":36,"sex":"Female","age":27,"classOfWorker":"State government","maritalStatus":"Never married","education":"12th grade no diploma","race":"Black"},{"id":37,"sex":"Male","age":68,"classOfWorker":"Not in universe","maritalStatus":"Widowed","education":"Less than 1st grade","race":"Other"},{"id":38,"sex":"Female","age":28,"classOfWorker":"Private","maritalStatus":"Never married","education":"11th grade","race":"Black"},{"id":39,"sex":"Male","age":54,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Masters degree(MA MS MEng MEd MSW MBA)","race":"White"},{"id":40,"sex":"Female","age":37,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":41,"sex":"Male","age":82,"classOfWorker":"Not in universe","maritalStatus":"Widowed","education":"7th and 8th grade","race":"White"},{"id":42,"sex":"Female","age":55,"classOfWorker":"Self-employed-not incorporated","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"Asian or Pacific Islander"},{"id":43,"sex":"Male","age":77,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"Less than 1st grade","race":"White"},{"id":44,"sex":"Male","age":53,"classOfWorker":"Private","maritalStatus":"Divorced","education":"Some college but no degree","race":"White"},{"id":45,"sex":"Male","age":25,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"White"},{"id":46,"sex":"Male","age":23,"classOfWorker":"Private","maritalStatus":"Never married","education":"Associates degree-academic program","race":"White"},{"id":47,"sex":"Female","age":0,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":48,"sex":"Female","age":49,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Some college but no degree","race":"White"},{"id":49,"sex":"Female","age":75,"classOfWorker":"Not in universe","maritalStatus":"Widowed","education":"10th grade","race":"White"},{"id":50,"sex":"Male","age":80,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"High school graduate","race":"White"},{"id":51,"sex":"Female","age":10,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"Asian or Pacific Islander"},{"id":52,"sex":"Male","age":22,"classOfWorker":"State government","maritalStatus":"Never married","education":"Associates degree-occup /vocational","race":"White"},{"id":53,"sex":"Female","age":61,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"9th grade","race":"White"},{"id":54,"sex":"Female","age":1,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":55,"sex":"Female","age":43,"classOfWorker":"Self-employed-not incorporated","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":56,"sex":"Male","age":48,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"Bachelors degree(BA AB BS)","race":"White"},{"id":57,"sex":"Female","age":5,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"Black"},{"id":58,"sex":"Female","age":16,"classOfWorker":"Never worked","maritalStatus":"Never married","education":"10th grade","race":"White"},{"id":59,"sex":"Female","age":27,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"High school graduate","race":"White"},{"id":60,"sex":"Male","age":61,"classOfWorker":"Self-employed-not incorporated","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":61,"sex":"Female","age":18,"classOfWorker":"Private","maritalStatus":"Never married","education":"11th grade","race":"Asian or Pacific Islander"},{"id":62,"sex":"Male","age":54,"classOfWorker":"Local government","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":63,"sex":"Male","age":50,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"7th and 8th grade","race":"White"},{"id":64,"sex":"Female","age":64,"classOfWorker":"Private","maritalStatus":"Widowed","education":"High school graduate","race":"White"},{"id":65,"sex":"Male","age":64,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"1st 2nd 3rd or 4th grade","race":"Black"},{"id":66,"sex":"Male","age":3,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":67,"sex":"Female","age":45,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"11th grade","race":"White"},{"id":68,"sex":"Female","age":72,"classOfWorker":"Not in universe","maritalStatus":"Widowed","education":"Bachelors degree(BA AB BS)","race":"White"},{"id":69,"sex":"Male","age":80,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":70,"sex":"Male","age":47,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Bachelors degree(BA AB BS)","race":"Black"},{"id":71,"sex":"Female","age":39,"classOfWorker":"Private","maritalStatus":"Divorced","education":"Associates degree-occup /vocational","race":"White"},{"id":72,"sex":"Male","age":51,"classOfWorker":"Self-employed-not incorporated","maritalStatus":"Married-civilian spouse present","education":"Some college but no degree","race":"White"},{"id":73,"sex":"Male","age":12,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"Asian or Pacific Islander"},{"id":74,"sex":"Male","age":41,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"Asian or Pacific Islander"},{"id":75,"sex":"Male","age":39,"classOfWorker":"Local government","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":76,"sex":"Female","age":67,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":77,"sex":"Female","age":59,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":78,"sex":"Female","age":48,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"10th grade","race":"White"},{"id":79,"sex":"Female","age":42,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"7th and 8th grade","race":"White"},{"id":80,"sex":"Male","age":38,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Some college but no degree","race":"White"},{"id":81,"sex":"Female","age":28,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Masters degree(MA MS MEng MEd MSW MBA)","race":"White"},{"id":82,"sex":"Male","age":8,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":83,"sex":"Female","age":4,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":84,"sex":"Male","age":26,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"White"},{"id":85,"sex":"Female","age":55,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"Associates degree-occup /vocational","race":"White"},{"id":86,"sex":"Female","age":42,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":87,"sex":"Male","age":18,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"12th grade no diploma","race":"White"},{"id":88,"sex":"Female","age":14,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":89,"sex":"Male","age":4,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":90,"sex":"Female","age":32,"classOfWorker":"Private","maritalStatus":"Never married","education":"Some college but no degree","race":"Black"},{"id":91,"sex":"Female","age":83,"classOfWorker":"Not in universe","maritalStatus":"Widowed","education":"7th and 8th grade","race":"White"},{"id":92,"sex":"Female","age":5,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":93,"sex":"Female","age":33,"classOfWorker":"Not in universe","maritalStatus":"Married-civilian spouse present","education":"Bachelors degree(BA AB BS)","race":"White"},{"id":94,"sex":"Female","age":57,"classOfWorker":"Self-employed-incorporated","maritalStatus":"Widowed","education":"12th grade no diploma","race":"White"},{"id":95,"sex":"Female","age":6,"classOfWorker":"Not in universe","maritalStatus":"Never married","education":"Children","race":"White"},{"id":96,"sex":"Female","age":37,"classOfWorker":"Local government","maritalStatus":"Married-civilian spouse present","education":"Bachelors degree(BA AB BS)","race":"White"},{"id":97,"sex":"Female","age":47,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":98,"sex":"Female","age":54,"classOfWorker":"Not in universe","maritalStatus":"Divorced","education":"9th grade","race":"White"},{"id":99,"sex":"Female","age":55,"classOfWorker":"Private","maritalStatus":"Married-civilian spouse present","education":"High school graduate","race":"White"},{"id":100,"sex":"Female","age":44,"classOfWorker":"Private","maritalStatus":"Divorced","education":"Some college but no degree","race":"White"}] 2 | var dummy50 = dummy100.slice(0, 50) 3 | var dummy10 = dummy100.slice(0, 10) 4 | 5 | function bench(method, dummy) { 6 | var t = Date.now(), i = 0 7 | while ((Date.now() - t) < 1000) { 8 | r = method(dummy) 9 | i++ 10 | } 11 | return i 12 | } 13 | 14 | var r 15 | var CircularJSON = require('circular-json') 16 | var jsan = require('jsan') 17 | var cj6 = require('./index') 18 | 19 | function run(name, fn, dummy) { 20 | console.log( 21 | name + ' ' + ( 22 | typeof dummy === 'string' ? 23 | dummy.length + ' chars' : 24 | dummy.length + ' objects' 25 | ) + ' parsed ' + ( 26 | bench(fn, dummy) / 1000 27 | ).toFixed(2) + ' times per second' 28 | ) 29 | } 30 | 31 | console.log('-----------------------------------') 32 | console.log('Object with ' + Object.keys(dummy100[0]).length + ' keys each') 33 | console.log('-----------------------------------') 34 | run('CircularJSON', CircularJSON.stringify, dummy100) 35 | run('CircularJSON', CircularJSON.parse, r) 36 | run('CircularJSON', CircularJSON.stringify, dummy50) 37 | run('CircularJSON', CircularJSON.parse, r) 38 | run('CircularJSON', CircularJSON.stringify, dummy10) 39 | run('CircularJSON', CircularJSON.parse, r) 40 | console.log('-----------------------------------') 41 | run('circular-json-es6', cj6.stringify, dummy100) 42 | run('circular-json-es6', cj6.parse, r) 43 | run('circular-json-es6', cj6.stringify, dummy50) 44 | run('circular-json-es6', cj6.parse, r) 45 | run('circular-json-es6', cj6.stringify, dummy10) 46 | run('circular-json-es6', cj6.parse, r) 47 | console.log('-----------------------------------') 48 | run('jsan', jsan.stringify, dummy100) 49 | run('jsan', jsan.parse, r) 50 | run('jsan', jsan.stringify, dummy50) 51 | run('jsan', jsan.parse, r) 52 | run('jsan', jsan.stringify, dummy10) 53 | run('jsan', jsan.parse, r) 54 | console.log('-----------------------------------') 55 | console.log('50% same objects') 56 | dummy100 = dummy50.concat(dummy50) 57 | console.log('-----------------------------------') 58 | run('CircularJSON', CircularJSON.stringify, dummy100) 59 | run('CircularJSON', CircularJSON.parse, r) 60 | run('circular-json-es6', cj6.stringify, dummy100) 61 | run('circular-json-es6', cj6.parse, r) 62 | run('jsan', jsan.stringify, dummy100) 63 | run('jsan', jsan.parse, r) 64 | console.log('-----------------------------------') 65 | console.log('90% same objects') 66 | dummy100 = [].concat( 67 | dummy10, dummy10, dummy10, dummy10, dummy10, 68 | dummy10, dummy10, dummy10, dummy10, dummy10 69 | ) 70 | console.log('-----------------------------------') 71 | run('CircularJSON', CircularJSON.stringify, dummy100) 72 | run('CircularJSON', CircularJSON.parse, r) 73 | run('circular-json-es6', cj6.stringify, dummy100) 74 | run('circular-json-es6', cj6.parse, r) 75 | run('jsan', jsan.stringify, dummy100) 76 | run('jsan', jsan.parse, r) 77 | console.log('-----------------------------------') 78 | console.log('with circular') 79 | function makeCircularObject () { 80 | var a = {} 81 | a.b = a 82 | return a 83 | } 84 | dummy100 = [] 85 | for (var i = 0; i < 100; i++) { 86 | dummy100.push(makeCircularObject()) 87 | } 88 | console.log('-----------------------------------') 89 | run('CircularJSON', CircularJSON.stringify, dummy100) 90 | run('CircularJSON', CircularJSON.parse, r) 91 | run('circular-json-es6', cj6.stringify, dummy100) 92 | run('circular-json-es6', cj6.parse, r) 93 | run('jsan', jsan.stringify, dummy100) 94 | run('jsan', jsan.parse, r) 95 | console.log('-----------------------------------') 96 | console.log('with circular 90% same') 97 | function makeCircularObject () { 98 | var a = {} 99 | a.b = a 100 | return a 101 | } 102 | dummy10 = [] 103 | for (var i = 0; i < 10; i++) { 104 | dummy10.push(makeCircularObject()) 105 | } 106 | dummy100 = [].concat( 107 | dummy10, dummy10, dummy10, dummy10, dummy10, 108 | dummy10, dummy10, dummy10, dummy10, dummy10 109 | ) 110 | console.log('-----------------------------------') 111 | run('CircularJSON', CircularJSON.stringify, dummy100) 112 | run('CircularJSON', CircularJSON.parse, r) 113 | run('circular-json-es6', cj6.stringify, dummy100) 114 | run('circular-json-es6', cj6.parse, r) 115 | run('jsan', jsan.stringify, dummy100) 116 | run('jsan', jsan.parse, r) 117 | --------------------------------------------------------------------------------