corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 // different ways to id objects
2 // use a req/res pair, since it's crazy deep and cyclical
3  
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.
7  
8  
9 var http = require('http')
10 var util = require('util')
11 var sigmund = require('./sigmund.js')
12 var sreq, sres, creq, cres, test
13  
14 http.createServer(function (q, s) {
15 sreq = q
16 sres = s
17 sres.end('ok')
18 this.close(function () { setTimeout(function () {
19 start()
20 }, 200) })
21 }).listen(1337, function () {
22 creq = http.get({ port: 1337 })
23 creq.on('response', function (s) { cres = s })
24 })
25  
26 function start () {
27 test = [sreq, sres, creq, cres]
28 // test = sreq
29 // sreq.sres = sres
30 // sreq.creq = creq
31 // sreq.cres = cres
32  
33 for (var i in exports.compare) {
34 console.log(i)
35 var hash = exports.compare[i]()
36 console.log(hash)
37 console.log(hash.length)
38 console.log('')
39 }
40  
41 require('bench').runMain()
42 }
43  
44 function customWs (obj, md, d) {
45 d = d || 0
46 var to = typeof obj
47 if (to === 'undefined' || to === 'function' || to === null) return ''
48 if (d > md || !obj || to !== 'object') return ('' + obj).replace(/[\n ]+/g, '')
49  
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 }, '')
54 }
55  
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 }, '')
60 }
61  
62 function custom (obj, md, d) {
63 d = d || 0
64 var to = typeof obj
65 if (to === 'undefined' || to === 'function' || to === null) return ''
66 if (d > md || !obj || to !== 'object') return '' + obj
67  
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 }, '')
72 }
73  
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 }, '')
78 }
79  
80 function sparseFE2 (obj, maxDepth) {
81 var seen = []
82 var soFar = ''
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) {
87 soFar += v
88 return
89 }
90 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
91 seen.push(v)
92 soFar += '{'
93 Object.keys(v).forEach(function (k, _, __) {
94 // pseudo-private values. skip those.
95 if (k.charAt(0) === '_') return
96 var to = typeof v[k]
97 if (to === 'function' || to === 'undefined') return
98 soFar += k + ':'
99 ch(v[k], depth + 1)
100 })
101 soFar += '}'
102 }
103 ch(obj, 0)
104 return soFar
105 }
106  
107 function sparseFE (obj, maxDepth) {
108 var seen = []
109 var soFar = ''
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) {
114 soFar += v
115 return
116 }
117 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
118 seen.push(v)
119 soFar += '{'
120 Object.keys(v).forEach(function (k, _, __) {
121 // pseudo-private values. skip those.
122 if (k.charAt(0) === '_') return
123 var to = typeof v[k]
124 if (to === 'function' || to === 'undefined') return
125 soFar += k
126 ch(v[k], depth + 1)
127 })
128 }
129 ch(obj, 0)
130 return soFar
131 }
132  
133 function sparse (obj, maxDepth) {
134 var seen = []
135 var soFar = ''
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) {
140 soFar += v
141 return
142 }
143 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
144 seen.push(v)
145 soFar += '{'
146 for (var k in v) {
147 // pseudo-private values. skip those.
148 if (k.charAt(0) === '_') continue
149 var to = typeof v[k]
150 if (to === 'function' || to === 'undefined') continue
151 soFar += k
152 ch(v[k], depth + 1)
153 }
154 }
155 ch(obj, 0)
156 return soFar
157 }
158  
159 function noCommas (obj, maxDepth) {
160 var seen = []
161 var soFar = ''
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) {
166 soFar += v
167 return
168 }
169 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
170 seen.push(v)
171 soFar += '{'
172 for (var k in v) {
173 // pseudo-private values. skip those.
174 if (k.charAt(0) === '_') continue
175 var to = typeof v[k]
176 if (to === 'function' || to === 'undefined') continue
177 soFar += k + ':'
178 ch(v[k], depth + 1)
179 }
180 soFar += '}'
181 }
182 ch(obj, 0)
183 return soFar
184 }
185  
186  
187 function flatten (obj, maxDepth) {
188 var seen = []
189 var soFar = ''
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) {
194 soFar += v
195 return
196 }
197 if (seen.indexOf(v) !== -1 || depth === maxDepth) return
198 seen.push(v)
199 soFar += '{'
200 for (var k in v) {
201 // pseudo-private values. skip those.
202 if (k.charAt(0) === '_') continue
203 var to = typeof v[k]
204 if (to === 'function' || to === 'undefined') continue
205 soFar += k + ':'
206 ch(v[k], depth + 1)
207 soFar += ','
208 }
209 soFar += '}'
210 }
211 ch(obj, 0)
212 return soFar
213 }
214  
215 exports.compare =
216 {
217 // 'custom 2': function () {
218 // return custom(test, 2, 0)
219 // },
220 // 'customWs 2': function () {
221 // return customWs(test, 2, 0)
222 // },
223 'JSON.stringify (guarded)': function () {
224 var seen = []
225 return JSON.stringify(test, function (k, v) {
226 if (typeof v !== 'object' || !v) return v
227 if (seen.indexOf(v) !== -1) return undefined
228 seen.push(v)
229 return v
230 })
231 },
232  
233 'flatten 10': function () {
234 return flatten(test, 10)
235 },
236  
237 // 'flattenFE 10': function () {
238 // return flattenFE(test, 10)
239 // },
240  
241 'noCommas 10': function () {
242 return noCommas(test, 10)
243 },
244  
245 'sparse 10': function () {
246 return sparse(test, 10)
247 },
248  
249 'sparseFE 10': function () {
250 return sparseFE(test, 10)
251 },
252  
253 'sparseFE2 10': function () {
254 return sparseFE2(test, 10)
255 },
256  
257 sigmund: function() {
258 return sigmund(test, 10)
259 },
260  
261  
262 // 'util.inspect 1': function () {
263 // return util.inspect(test, false, 1, false)
264 // },
265 // 'util.inspect undefined': function () {
266 // util.inspect(test)
267 // },
268 // 'util.inspect 2': function () {
269 // util.inspect(test, false, 2, false)
270 // },
271 // 'util.inspect 3': function () {
272 // util.inspect(test, false, 3, false)
273 // },
274 // 'util.inspect 4': function () {
275 // util.inspect(test, false, 4, false)
276 // },
277 // 'util.inspect Infinity': function () {
278 // util.inspect(test, false, Infinity, false)
279 // }
280 }
281  
282 /** results
283 **/