1 // different ways to id objects
2 // use a req/res pair, since it's crazy deep and cyclical
4 // sparseFE10 and sigmund are usually pretty close, which is to be expected,
5 // since they are essentially the same algorithm, except that sigmund handles
6 // regular expression objects properly.
9 var http = require('http')
10 var util = require('util')
11 var sigmund = require('./sigmund.js')
12 var sreq, sres, creq, cres, test
14 http.createServer(function (q, s) {
18 this.close(function () { setTimeout(function () {
21 }).listen(1337, function () {
22 creq = http.get({ port: 1337 })
23 creq.on('response', function (s) { cres = s })
27 test = [sreq, sres, creq, cres]
33 for (var i in exports.compare) {
35 var hash = exports.compare[i]()
37 console.log(hash.length)
41 require('bench').runMain()
44 function customWs (obj, md, d) {
47 if (to === 'undefined' || to === 'function' || to === null) return ''
48 if (d > md || !obj || to !== 'object') return ('' + obj).replace(/[\n ]+/g, '')
50 if (Array.isArray(obj)) {
51 return obj.map(function (i, _, __) {
52 return customWs(i, md, d + 1)
53 }).reduce(function (a, b) { return a + b }, '')
56 var keys = Object.keys(obj)
57 return keys.map(function (k, _, __) {
58 return k + ':' + customWs(obj[k], md, d + 1)
59 }).reduce(function (a, b) { return a + b }, '')
62 function custom (obj, md, d) {
65 if (to === 'undefined' || to === 'function' || to === null) return ''
66 if (d > md || !obj || to !== 'object') return '' + obj
68 if (Array.isArray(obj)) {
69 return obj.map(function (i, _, __) {
70 return custom(i, md, d + 1)
71 }).reduce(function (a, b) { return a + b }, '')
74 var keys = Object.keys(obj)
75 return keys.map(function (k, _, __) {
76 return k + ':' + custom(obj[k], md, d + 1)
77 }).reduce(function (a, b) { return a + b }, '')
80 function sparseFE2 (obj, maxDepth) {
83 function ch (v, depth) {
84 if (depth > maxDepth) return
85 if (typeof v === 'function' || typeof v === 'undefined') return
86 if (typeof v !== 'object' || !v) {
90 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
93 Object.keys(v).forEach(function (k, _, __) {
94 // pseudo-private values. skip those.
95 if (k.charAt(0) === '_') return
97 if (to === 'function' || to === 'undefined') return
107 function sparseFE (obj, maxDepth) {
110 function ch (v, depth) {
111 if (depth > maxDepth) return
112 if (typeof v === 'function' || typeof v === 'undefined') return
113 if (typeof v !== 'object' || !v) {
117 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
120 Object.keys(v).forEach(function (k, _, __) {
121 // pseudo-private values. skip those.
122 if (k.charAt(0) === '_') return
124 if (to === 'function' || to === 'undefined') return
133 function sparse (obj, maxDepth) {
136 function ch (v, depth) {
137 if (depth > maxDepth) return
138 if (typeof v === 'function' || typeof v === 'undefined') return
139 if (typeof v !== 'object' || !v) {
143 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
147 // pseudo-private values. skip those.
148 if (k.charAt(0) === '_') continue
150 if (to === 'function' || to === 'undefined') continue
159 function noCommas (obj, maxDepth) {
162 function ch (v, depth) {
163 if (depth > maxDepth) return
164 if (typeof v === 'function' || typeof v === 'undefined') return
165 if (typeof v !== 'object' || !v) {
169 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
173 // pseudo-private values. skip those.
174 if (k.charAt(0) === '_') continue
176 if (to === 'function' || to === 'undefined') continue
187 function flatten (obj, maxDepth) {
190 function ch (v, depth) {
191 if (depth > maxDepth) return
192 if (typeof v === 'function' || typeof v === 'undefined') return
193 if (typeof v !== 'object' || !v) {
197 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
201 // pseudo-private values. skip those.
202 if (k.charAt(0) === '_') continue
204 if (to === 'function' || to === 'undefined') continue
217 // 'custom 2': function () {
218 // return custom(test, 2, 0)
220 // 'customWs 2': function () {
221 // return customWs(test, 2, 0)
223 'JSON.stringify (guarded)': function () {
225 return JSON.stringify(test, function (k, v) {
226 if (typeof v !== 'object' || !v) return v
227 if (seen.indexOf(v) !== -1) return undefined
233 'flatten 10': function () {
234 return flatten(test, 10)
237 // 'flattenFE 10': function () {
238 // return flattenFE(test, 10)
241 'noCommas 10': function () {
242 return noCommas(test, 10)
245 'sparse 10': function () {
246 return sparse(test, 10)
249 'sparseFE 10': function () {
250 return sparseFE(test, 10)
253 'sparseFE2 10': function () {
254 return sparseFE2(test, 10)
257 sigmund: function() {
258 return sigmund(test, 10)
262 // 'util.inspect 1': function () {
263 // return util.inspect(test, false, 1, false)
265 // 'util.inspect undefined': function () {
266 // util.inspect(test)
268 // 'util.inspect 2': function () {
269 // util.inspect(test, false, 2, false)
271 // 'util.inspect 3': function () {
272 // util.inspect(test, false, 3, false)
274 // 'util.inspect 4': function () {
275 // util.inspect(test, false, 4, false)
277 // 'util.inspect Infinity': function () {
278 // util.inspect(test, false, Infinity, false)