bench.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // different ways to id objects
  2. // use a req/res pair, since it's crazy deep and cyclical
  3. // sparseFE10 and sigmund are usually pretty close, which is to be expected,
  4. // since they are essentially the same algorithm, except that sigmund handles
  5. // regular expression objects properly.
  6. var http = require('http')
  7. var util = require('util')
  8. var sigmund = require('./sigmund.js')
  9. var sreq, sres, creq, cres, test
  10. http.createServer(function (q, s) {
  11. sreq = q
  12. sres = s
  13. sres.end('ok')
  14. this.close(function () { setTimeout(function () {
  15. start()
  16. }, 200) })
  17. }).listen(1337, function () {
  18. creq = http.get({ port: 1337 })
  19. creq.on('response', function (s) { cres = s })
  20. })
  21. function start () {
  22. test = [sreq, sres, creq, cres]
  23. // test = sreq
  24. // sreq.sres = sres
  25. // sreq.creq = creq
  26. // sreq.cres = cres
  27. for (var i in exports.compare) {
  28. console.log(i)
  29. var hash = exports.compare[i]()
  30. console.log(hash)
  31. console.log(hash.length)
  32. console.log('')
  33. }
  34. require('bench').runMain()
  35. }
  36. function customWs (obj, md, d) {
  37. d = d || 0
  38. var to = typeof obj
  39. if (to === 'undefined' || to === 'function' || to === null) return ''
  40. if (d > md || !obj || to !== 'object') return ('' + obj).replace(/[\n ]+/g, '')
  41. if (Array.isArray(obj)) {
  42. return obj.map(function (i, _, __) {
  43. return customWs(i, md, d + 1)
  44. }).reduce(function (a, b) { return a + b }, '')
  45. }
  46. var keys = Object.keys(obj)
  47. return keys.map(function (k, _, __) {
  48. return k + ':' + customWs(obj[k], md, d + 1)
  49. }).reduce(function (a, b) { return a + b }, '')
  50. }
  51. function custom (obj, md, d) {
  52. d = d || 0
  53. var to = typeof obj
  54. if (to === 'undefined' || to === 'function' || to === null) return ''
  55. if (d > md || !obj || to !== 'object') return '' + obj
  56. if (Array.isArray(obj)) {
  57. return obj.map(function (i, _, __) {
  58. return custom(i, md, d + 1)
  59. }).reduce(function (a, b) { return a + b }, '')
  60. }
  61. var keys = Object.keys(obj)
  62. return keys.map(function (k, _, __) {
  63. return k + ':' + custom(obj[k], md, d + 1)
  64. }).reduce(function (a, b) { return a + b }, '')
  65. }
  66. function sparseFE2 (obj, maxDepth) {
  67. var seen = []
  68. var soFar = ''
  69. function ch (v, depth) {
  70. if (depth > maxDepth) return
  71. if (typeof v === 'function' || typeof v === 'undefined') return
  72. if (typeof v !== 'object' || !v) {
  73. soFar += v
  74. return
  75. }
  76. if (seen.indexOf(v) !== -1 || depth === maxDepth) return
  77. seen.push(v)
  78. soFar += '{'
  79. Object.keys(v).forEach(function (k, _, __) {
  80. // pseudo-private values. skip those.
  81. if (k.charAt(0) === '_') return
  82. var to = typeof v[k]
  83. if (to === 'function' || to === 'undefined') return
  84. soFar += k + ':'
  85. ch(v[k], depth + 1)
  86. })
  87. soFar += '}'
  88. }
  89. ch(obj, 0)
  90. return soFar
  91. }
  92. function sparseFE (obj, maxDepth) {
  93. var seen = []
  94. var soFar = ''
  95. function ch (v, depth) {
  96. if (depth > maxDepth) return
  97. if (typeof v === 'function' || typeof v === 'undefined') return
  98. if (typeof v !== 'object' || !v) {
  99. soFar += v
  100. return
  101. }
  102. if (seen.indexOf(v) !== -1 || depth === maxDepth) return
  103. seen.push(v)
  104. soFar += '{'
  105. Object.keys(v).forEach(function (k, _, __) {
  106. // pseudo-private values. skip those.
  107. if (k.charAt(0) === '_') return
  108. var to = typeof v[k]
  109. if (to === 'function' || to === 'undefined') return
  110. soFar += k
  111. ch(v[k], depth + 1)
  112. })
  113. }
  114. ch(obj, 0)
  115. return soFar
  116. }
  117. function sparse (obj, maxDepth) {
  118. var seen = []
  119. var soFar = ''
  120. function ch (v, depth) {
  121. if (depth > maxDepth) return
  122. if (typeof v === 'function' || typeof v === 'undefined') return
  123. if (typeof v !== 'object' || !v) {
  124. soFar += v
  125. return
  126. }
  127. if (seen.indexOf(v) !== -1 || depth === maxDepth) return
  128. seen.push(v)
  129. soFar += '{'
  130. for (var k in v) {
  131. // pseudo-private values. skip those.
  132. if (k.charAt(0) === '_') continue
  133. var to = typeof v[k]
  134. if (to === 'function' || to === 'undefined') continue
  135. soFar += k
  136. ch(v[k], depth + 1)
  137. }
  138. }
  139. ch(obj, 0)
  140. return soFar
  141. }
  142. function noCommas (obj, maxDepth) {
  143. var seen = []
  144. var soFar = ''
  145. function ch (v, depth) {
  146. if (depth > maxDepth) return
  147. if (typeof v === 'function' || typeof v === 'undefined') return
  148. if (typeof v !== 'object' || !v) {
  149. soFar += v
  150. return
  151. }
  152. if (seen.indexOf(v) !== -1 || depth === maxDepth) return
  153. seen.push(v)
  154. soFar += '{'
  155. for (var k in v) {
  156. // pseudo-private values. skip those.
  157. if (k.charAt(0) === '_') continue
  158. var to = typeof v[k]
  159. if (to === 'function' || to === 'undefined') continue
  160. soFar += k + ':'
  161. ch(v[k], depth + 1)
  162. }
  163. soFar += '}'
  164. }
  165. ch(obj, 0)
  166. return soFar
  167. }
  168. function flatten (obj, maxDepth) {
  169. var seen = []
  170. var soFar = ''
  171. function ch (v, depth) {
  172. if (depth > maxDepth) return
  173. if (typeof v === 'function' || typeof v === 'undefined') return
  174. if (typeof v !== 'object' || !v) {
  175. soFar += v
  176. return
  177. }
  178. if (seen.indexOf(v) !== -1 || depth === maxDepth) return
  179. seen.push(v)
  180. soFar += '{'
  181. for (var k in v) {
  182. // pseudo-private values. skip those.
  183. if (k.charAt(0) === '_') continue
  184. var to = typeof v[k]
  185. if (to === 'function' || to === 'undefined') continue
  186. soFar += k + ':'
  187. ch(v[k], depth + 1)
  188. soFar += ','
  189. }
  190. soFar += '}'
  191. }
  192. ch(obj, 0)
  193. return soFar
  194. }
  195. exports.compare =
  196. {
  197. // 'custom 2': function () {
  198. // return custom(test, 2, 0)
  199. // },
  200. // 'customWs 2': function () {
  201. // return customWs(test, 2, 0)
  202. // },
  203. 'JSON.stringify (guarded)': function () {
  204. var seen = []
  205. return JSON.stringify(test, function (k, v) {
  206. if (typeof v !== 'object' || !v) return v
  207. if (seen.indexOf(v) !== -1) return undefined
  208. seen.push(v)
  209. return v
  210. })
  211. },
  212. 'flatten 10': function () {
  213. return flatten(test, 10)
  214. },
  215. // 'flattenFE 10': function () {
  216. // return flattenFE(test, 10)
  217. // },
  218. 'noCommas 10': function () {
  219. return noCommas(test, 10)
  220. },
  221. 'sparse 10': function () {
  222. return sparse(test, 10)
  223. },
  224. 'sparseFE 10': function () {
  225. return sparseFE(test, 10)
  226. },
  227. 'sparseFE2 10': function () {
  228. return sparseFE2(test, 10)
  229. },
  230. sigmund: function() {
  231. return sigmund(test, 10)
  232. },
  233. // 'util.inspect 1': function () {
  234. // return util.inspect(test, false, 1, false)
  235. // },
  236. // 'util.inspect undefined': function () {
  237. // util.inspect(test)
  238. // },
  239. // 'util.inspect 2': function () {
  240. // util.inspect(test, false, 2, false)
  241. // },
  242. // 'util.inspect 3': function () {
  243. // util.inspect(test, false, 3, false)
  244. // },
  245. // 'util.inspect 4': function () {
  246. // util.inspect(test, false, 4, false)
  247. // },
  248. // 'util.inspect Infinity': function () {
  249. // util.inspect(test, false, Infinity, false)
  250. // }
  251. }
  252. /** results
  253. **/