node_modules/sigmund/bench.js

Maintainability

69.37

Lines of code

275

Created with Raphaël 2.1.002550751002015-1-52014-12-42014-12-3

2015-5-18
Maintainability: 69.37

Created with Raphaël 2.1.00751502253002015-1-52014-12-42014-12-3

2015-5-18
Lines of Code: 275

Difficulty

64.68

Estimated Errors

2.51

Function weight

By Complexity

Created with Raphaël 2.1.0ch11

By SLOC

Created with Raphaël 2.1.0flatten27
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')
Column: 27 "Missing semicolon."
10
var util = require('util')
Column: 27 "Missing semicolon."
11
var sigmund = require('./sigmund.js')
Column: 38 "Missing semicolon."
12
var sreq, sres, creq, cres, test
Column: 33 "Missing semicolon."
13
 
14
http.createServer(function (q, s) {
15
  sreq = q
Column: 11 "Missing semicolon."
16
  sres = s
Column: 11 "Missing semicolon."
17
  sres.end('ok')
Column: 17 "Missing semicolon."
18
  this.close(function () { setTimeout(function () {
19
    start()
Column: 12 "Missing semicolon."
20
  }, 200) })
Column: 10 "Missing semicolon."
Column: 13 "Missing semicolon."
21
}).listen(1337, function () {
22
  creq = http.get({ port: 1337 })
Column: 34 "Missing semicolon."
23
  creq.on('response', function (s) { cres = s })
Column: 46 "Missing semicolon."
Column: 49 "Missing semicolon."
24
})
Column: 3 "Missing semicolon."
25
 
26
function start () {
27
  test = [sreq, sres, creq, cres]
Column: 34 "Missing semicolon."
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)
Column: 19 "Missing semicolon."
35
    var hash = exports.compare[i]()
Column: 36 "Missing semicolon."
36
    console.log(hash)
Column: 22 "Missing semicolon."
37
    console.log(hash.length)
Column: 29 "Missing semicolon."
38
    console.log('')
Column: 20 "Missing semicolon."
39
  }
40
 
41
  require('bench').runMain()
Column: 29 "Missing semicolon."
42
}
43
 
44
function customWs (obj, md, d) {
45
  d = d || 0
Column: 13 "Missing semicolon."
46
  var to = typeof obj
Column: 22 "Missing semicolon."
47
  if (to === 'undefined' || to === 'function' || to === null) return ''
Column: 72 "Missing semicolon."
48
  if (d > md || !obj || to !== 'object') return ('' + obj).replace(/[\n ]+/g, '')
Column: 82 "Missing semicolon."
49
 
50
  if (Array.isArray(obj)) {
51
    return obj.map(function (i, _, __) {
52
      return customWs(i, md, d + 1)
Column: 36 "Missing semicolon."
53
    }).reduce(function (a, b) { return a + b }, '')
Column: 45 "Missing semicolon."
Column: 52 "Missing semicolon."
54
  }
55
 
56
  var keys = Object.keys(obj)
Column: 30 "Missing semicolon."
57
  return keys.map(function (k, _, __) {
58
    return k + ':' + customWs(obj[k], md, d + 1)
Column: 49 "Missing semicolon."
59
  }).reduce(function (a, b) { return a + b }, '')
Column: 43 "Missing semicolon."
Column: 50 "Missing semicolon."
60
}
61
 
62
function custom (obj, md, d) {
63
  d = d || 0
Column: 13 "Missing semicolon."
64
  var to = typeof obj
Column: 22 "Missing semicolon."
65
  if (to === 'undefined' || to === 'function' || to === null) return ''
Column: 72 "Missing semicolon."
66
  if (d > md || !obj || to !== 'object') return '' + obj
Column: 57 "Missing semicolon."
67
 
68
  if (Array.isArray(obj)) {
69
    return obj.map(function (i, _, __) {
70
      return custom(i, md, d + 1)
Column: 34 "Missing semicolon."
71
    }).reduce(function (a, b) { return a + b }, '')
Column: 45 "Missing semicolon."
Column: 52 "Missing semicolon."
72
  }
73
 
74
  var keys = Object.keys(obj)
Column: 30 "Missing semicolon."
75
  return keys.map(function (k, _, __) {
76
    return k + ':' + custom(obj[k], md, d + 1)
Column: 47 "Missing semicolon."
77
  }).reduce(function (a, b) { return a + b }, '')
Column: 43 "Missing semicolon."
Column: 50 "Missing semicolon."
78
}
79
 
80
function sparseFE2 (obj, maxDepth) {
81
  var seen = []
Column: 16 "Missing semicolon."
82
  var soFar = ''
Column: 17 "Missing semicolon."
83
  function ch (v, depth) {
84
    if (depth > maxDepth) return
Column: 33 "Missing semicolon."
85
    if (typeof v === 'function' || typeof v === 'undefined') return
Column: 68 "Missing semicolon."
86
    if (typeof v !== 'object' || !v) {
87
      soFar += v
Column: 17 "Missing semicolon."
88
      return
Column: 13 "Missing semicolon."
89
    }
90
    if (seen.indexOf(v) !== -1 || depth === maxDepth) return
Column: 61 "Missing semicolon."
Column: 61 "Too many errors. (31% scanned)."
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
**/