react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / fileset / node_modules / glob / node_modules / minimatch / node_modules / sigmund / bench.js
80742 views// different ways to id objects1// use a req/res pair, since it's crazy deep and cyclical23// sparseFE10 and sigmund are usually pretty close, which is to be expected,4// since they are essentially the same algorithm, except that sigmund handles5// regular expression objects properly.678var http = require('http')9var util = require('util')10var sigmund = require('./sigmund.js')11var sreq, sres, creq, cres, test1213http.createServer(function (q, s) {14sreq = q15sres = s16sres.end('ok')17this.close(function () { setTimeout(function () {18start()19}, 200) })20}).listen(1337, function () {21creq = http.get({ port: 1337 })22creq.on('response', function (s) { cres = s })23})2425function start () {26test = [sreq, sres, creq, cres]27// test = sreq28// sreq.sres = sres29// sreq.creq = creq30// sreq.cres = cres3132for (var i in exports.compare) {33console.log(i)34var hash = exports.compare[i]()35console.log(hash)36console.log(hash.length)37console.log('')38}3940require('bench').runMain()41}4243function customWs (obj, md, d) {44d = d || 045var to = typeof obj46if (to === 'undefined' || to === 'function' || to === null) return ''47if (d > md || !obj || to !== 'object') return ('' + obj).replace(/[\n ]+/g, '')4849if (Array.isArray(obj)) {50return obj.map(function (i, _, __) {51return customWs(i, md, d + 1)52}).reduce(function (a, b) { return a + b }, '')53}5455var keys = Object.keys(obj)56return keys.map(function (k, _, __) {57return k + ':' + customWs(obj[k], md, d + 1)58}).reduce(function (a, b) { return a + b }, '')59}6061function custom (obj, md, d) {62d = d || 063var to = typeof obj64if (to === 'undefined' || to === 'function' || to === null) return ''65if (d > md || !obj || to !== 'object') return '' + obj6667if (Array.isArray(obj)) {68return obj.map(function (i, _, __) {69return custom(i, md, d + 1)70}).reduce(function (a, b) { return a + b }, '')71}7273var keys = Object.keys(obj)74return keys.map(function (k, _, __) {75return k + ':' + custom(obj[k], md, d + 1)76}).reduce(function (a, b) { return a + b }, '')77}7879function sparseFE2 (obj, maxDepth) {80var seen = []81var soFar = ''82function ch (v, depth) {83if (depth > maxDepth) return84if (typeof v === 'function' || typeof v === 'undefined') return85if (typeof v !== 'object' || !v) {86soFar += v87return88}89if (seen.indexOf(v) !== -1 || depth === maxDepth) return90seen.push(v)91soFar += '{'92Object.keys(v).forEach(function (k, _, __) {93// pseudo-private values. skip those.94if (k.charAt(0) === '_') return95var to = typeof v[k]96if (to === 'function' || to === 'undefined') return97soFar += k + ':'98ch(v[k], depth + 1)99})100soFar += '}'101}102ch(obj, 0)103return soFar104}105106function sparseFE (obj, maxDepth) {107var seen = []108var soFar = ''109function ch (v, depth) {110if (depth > maxDepth) return111if (typeof v === 'function' || typeof v === 'undefined') return112if (typeof v !== 'object' || !v) {113soFar += v114return115}116if (seen.indexOf(v) !== -1 || depth === maxDepth) return117seen.push(v)118soFar += '{'119Object.keys(v).forEach(function (k, _, __) {120// pseudo-private values. skip those.121if (k.charAt(0) === '_') return122var to = typeof v[k]123if (to === 'function' || to === 'undefined') return124soFar += k125ch(v[k], depth + 1)126})127}128ch(obj, 0)129return soFar130}131132function sparse (obj, maxDepth) {133var seen = []134var soFar = ''135function ch (v, depth) {136if (depth > maxDepth) return137if (typeof v === 'function' || typeof v === 'undefined') return138if (typeof v !== 'object' || !v) {139soFar += v140return141}142if (seen.indexOf(v) !== -1 || depth === maxDepth) return143seen.push(v)144soFar += '{'145for (var k in v) {146// pseudo-private values. skip those.147if (k.charAt(0) === '_') continue148var to = typeof v[k]149if (to === 'function' || to === 'undefined') continue150soFar += k151ch(v[k], depth + 1)152}153}154ch(obj, 0)155return soFar156}157158function noCommas (obj, maxDepth) {159var seen = []160var soFar = ''161function ch (v, depth) {162if (depth > maxDepth) return163if (typeof v === 'function' || typeof v === 'undefined') return164if (typeof v !== 'object' || !v) {165soFar += v166return167}168if (seen.indexOf(v) !== -1 || depth === maxDepth) return169seen.push(v)170soFar += '{'171for (var k in v) {172// pseudo-private values. skip those.173if (k.charAt(0) === '_') continue174var to = typeof v[k]175if (to === 'function' || to === 'undefined') continue176soFar += k + ':'177ch(v[k], depth + 1)178}179soFar += '}'180}181ch(obj, 0)182return soFar183}184185186function flatten (obj, maxDepth) {187var seen = []188var soFar = ''189function ch (v, depth) {190if (depth > maxDepth) return191if (typeof v === 'function' || typeof v === 'undefined') return192if (typeof v !== 'object' || !v) {193soFar += v194return195}196if (seen.indexOf(v) !== -1 || depth === maxDepth) return197seen.push(v)198soFar += '{'199for (var k in v) {200// pseudo-private values. skip those.201if (k.charAt(0) === '_') continue202var to = typeof v[k]203if (to === 'function' || to === 'undefined') continue204soFar += k + ':'205ch(v[k], depth + 1)206soFar += ','207}208soFar += '}'209}210ch(obj, 0)211return soFar212}213214exports.compare =215{216// 'custom 2': function () {217// return custom(test, 2, 0)218// },219// 'customWs 2': function () {220// return customWs(test, 2, 0)221// },222'JSON.stringify (guarded)': function () {223var seen = []224return JSON.stringify(test, function (k, v) {225if (typeof v !== 'object' || !v) return v226if (seen.indexOf(v) !== -1) return undefined227seen.push(v)228return v229})230},231232'flatten 10': function () {233return flatten(test, 10)234},235236// 'flattenFE 10': function () {237// return flattenFE(test, 10)238// },239240'noCommas 10': function () {241return noCommas(test, 10)242},243244'sparse 10': function () {245return sparse(test, 10)246},247248'sparseFE 10': function () {249return sparseFE(test, 10)250},251252'sparseFE2 10': function () {253return sparseFE2(test, 10)254},255256sigmund: function() {257return sigmund(test, 10)258},259260261// 'util.inspect 1': function () {262// return util.inspect(test, false, 1, false)263// },264// 'util.inspect undefined': function () {265// util.inspect(test)266// },267// 'util.inspect 2': function () {268// util.inspect(test, false, 2, false)269// },270// 'util.inspect 3': function () {271// util.inspect(test, false, 3, false)272// },273// 'util.inspect 4': function () {274// util.inspect(test, false, 4, false)275// },276// 'util.inspect Infinity': function () {277// util.inspect(test, false, Infinity, false)278// }279}280281/** results282**/283284285