UNPKG

circular-json-es6

Version:

circular JSON.stringify and JSON.parse, for environments with native ES6 Map

117 lines (112 loc) 19.2 kB
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"}] var dummy50 = dummy100.slice(0, 50) var dummy10 = dummy100.slice(0, 10) function bench(method, dummy) { var t = Date.now(), i = 0 while ((Date.now() - t) < 1000) { r = method(dummy) i++ } return i } var r var CircularJSON = require('circular-json') var jsan = require('jsan') var cj6 = require('./index') function run(name, fn, dummy) { console.log( name + ' ' + ( typeof dummy === 'string' ? dummy.length + ' chars' : dummy.length + ' objects' ) + ' parsed ' + ( bench(fn, dummy) / 1000 ).toFixed(2) + ' times per second' ) } console.log('-----------------------------------') console.log('Object with ' + Object.keys(dummy100[0]).length + ' keys each') console.log('-----------------------------------') run('CircularJSON', CircularJSON.stringify, dummy100) run('CircularJSON', CircularJSON.parse, r) run('CircularJSON', CircularJSON.stringify, dummy50) run('CircularJSON', CircularJSON.parse, r) run('CircularJSON', CircularJSON.stringify, dummy10) run('CircularJSON', CircularJSON.parse, r) console.log('-----------------------------------') run('circular-json-es6', cj6.stringify, dummy100) run('circular-json-es6', cj6.parse, r) run('circular-json-es6', cj6.stringify, dummy50) run('circular-json-es6', cj6.parse, r) run('circular-json-es6', cj6.stringify, dummy10) run('circular-json-es6', cj6.parse, r) console.log('-----------------------------------') run('jsan', jsan.stringify, dummy100) run('jsan', jsan.parse, r) run('jsan', jsan.stringify, dummy50) run('jsan', jsan.parse, r) run('jsan', jsan.stringify, dummy10) run('jsan', jsan.parse, r) console.log('-----------------------------------') console.log('50% same objects') dummy100 = dummy50.concat(dummy50) console.log('-----------------------------------') run('CircularJSON', CircularJSON.stringify, dummy100) run('CircularJSON', CircularJSON.parse, r) run('circular-json-es6', cj6.stringify, dummy100) run('circular-json-es6', cj6.parse, r) run('jsan', jsan.stringify, dummy100) run('jsan', jsan.parse, r) console.log('-----------------------------------') console.log('90% same objects') dummy100 = [].concat( dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10 ) console.log('-----------------------------------') run('CircularJSON', CircularJSON.stringify, dummy100) run('CircularJSON', CircularJSON.parse, r) run('circular-json-es6', cj6.stringify, dummy100) run('circular-json-es6', cj6.parse, r) run('jsan', jsan.stringify, dummy100) run('jsan', jsan.parse, r) console.log('-----------------------------------') console.log('with circular') function makeCircularObject () { var a = {} a.b = a return a } dummy100 = [] for (var i = 0; i < 100; i++) { dummy100.push(makeCircularObject()) } console.log('-----------------------------------') run('CircularJSON', CircularJSON.stringify, dummy100) run('CircularJSON', CircularJSON.parse, r) run('circular-json-es6', cj6.stringify, dummy100) run('circular-json-es6', cj6.parse, r) run('jsan', jsan.stringify, dummy100) run('jsan', jsan.parse, r) console.log('-----------------------------------') console.log('with circular 90% same') function makeCircularObject () { var a = {} a.b = a return a } dummy10 = [] for (var i = 0; i < 10; i++) { dummy10.push(makeCircularObject()) } dummy100 = [].concat( dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10, dummy10 ) console.log('-----------------------------------') run('CircularJSON', CircularJSON.stringify, dummy100) run('CircularJSON', CircularJSON.parse, r) run('circular-json-es6', cj6.stringify, dummy100) run('circular-json-es6', cj6.parse, r) run('jsan', jsan.stringify, dummy100) run('jsan', jsan.parse, r)