benchmark.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const Benchmark = require('benchmark')
  2. const suite = new Benchmark.Suite()
  3. const { inspect } = require('util')
  4. const jsonStringifySafe = require('json-stringify-safe')
  5. const fastSafeStringify = require('./')
  6. const array = new Array(10).fill(0).map((_, i) => i)
  7. const obj = { foo: array }
  8. const circ = JSON.parse(JSON.stringify(obj))
  9. circ.o = { obj: circ, array }
  10. const circGetters = JSON.parse(JSON.stringify(obj))
  11. Object.assign(circGetters, { get o () { return { obj: circGetters, array } } })
  12. const deep = require('./package.json')
  13. deep.deep = JSON.parse(JSON.stringify(deep))
  14. deep.deep.deep = JSON.parse(JSON.stringify(deep))
  15. deep.deep.deep.deep = JSON.parse(JSON.stringify(deep))
  16. deep.array = array
  17. const deepCirc = JSON.parse(JSON.stringify(deep))
  18. deepCirc.deep.deep.deep.circ = deepCirc
  19. deepCirc.deep.deep.circ = deepCirc
  20. deepCirc.deep.circ = deepCirc
  21. deepCirc.array = array
  22. const deepCircGetters = JSON.parse(JSON.stringify(deep))
  23. for (let i = 0; i < 10; i++) {
  24. deepCircGetters[i.toString()] = {
  25. deep: {
  26. deep: {
  27. get circ () { return deep.deep },
  28. deep: { get circ () { return deep.deep.deep } }
  29. },
  30. get circ () { return deep }
  31. },
  32. get array () { return array }
  33. }
  34. }
  35. const deepCircNonCongifurableGetters = JSON.parse(JSON.stringify(deep))
  36. Object.defineProperty(deepCircNonCongifurableGetters.deep.deep.deep, 'circ', {
  37. get: () => deepCircNonCongifurableGetters,
  38. enumerable: true,
  39. configurable: false
  40. })
  41. Object.defineProperty(deepCircNonCongifurableGetters.deep.deep, 'circ', {
  42. get: () => deepCircNonCongifurableGetters,
  43. enumerable: true,
  44. configurable: false
  45. })
  46. Object.defineProperty(deepCircNonCongifurableGetters.deep, 'circ', {
  47. get: () => deepCircNonCongifurableGetters,
  48. enumerable: true,
  49. configurable: false
  50. })
  51. Object.defineProperty(deepCircNonCongifurableGetters, 'array', {
  52. get: () => array,
  53. enumerable: true,
  54. configurable: false
  55. })
  56. suite.add('util.inspect: simple object ', function () {
  57. inspect(obj, { showHidden: false, depth: null })
  58. })
  59. suite.add('util.inspect: circular ', function () {
  60. inspect(circ, { showHidden: false, depth: null })
  61. })
  62. suite.add('util.inspect: circular getters ', function () {
  63. inspect(circGetters, { showHidden: false, depth: null })
  64. })
  65. suite.add('util.inspect: deep ', function () {
  66. inspect(deep, { showHidden: false, depth: null })
  67. })
  68. suite.add('util.inspect: deep circular ', function () {
  69. inspect(deepCirc, { showHidden: false, depth: null })
  70. })
  71. suite.add('util.inspect: large deep circular getters ', function () {
  72. inspect(deepCircGetters, { showHidden: false, depth: null })
  73. })
  74. suite.add('util.inspect: deep non-conf circular getters', function () {
  75. inspect(deepCircNonCongifurableGetters, { showHidden: false, depth: null })
  76. })
  77. suite.add('\njson-stringify-safe: simple object ', function () {
  78. jsonStringifySafe(obj)
  79. })
  80. suite.add('json-stringify-safe: circular ', function () {
  81. jsonStringifySafe(circ)
  82. })
  83. suite.add('json-stringify-safe: circular getters ', function () {
  84. jsonStringifySafe(circGetters)
  85. })
  86. suite.add('json-stringify-safe: deep ', function () {
  87. jsonStringifySafe(deep)
  88. })
  89. suite.add('json-stringify-safe: deep circular ', function () {
  90. jsonStringifySafe(deepCirc)
  91. })
  92. suite.add('json-stringify-safe: large deep circular getters ', function () {
  93. jsonStringifySafe(deepCircGetters)
  94. })
  95. suite.add('json-stringify-safe: deep non-conf circular getters', function () {
  96. jsonStringifySafe(deepCircNonCongifurableGetters)
  97. })
  98. suite.add('\nfast-safe-stringify: simple object ', function () {
  99. fastSafeStringify(obj)
  100. })
  101. suite.add('fast-safe-stringify: circular ', function () {
  102. fastSafeStringify(circ)
  103. })
  104. suite.add('fast-safe-stringify: circular getters ', function () {
  105. fastSafeStringify(circGetters)
  106. })
  107. suite.add('fast-safe-stringify: deep ', function () {
  108. fastSafeStringify(deep)
  109. })
  110. suite.add('fast-safe-stringify: deep circular ', function () {
  111. fastSafeStringify(deepCirc)
  112. })
  113. suite.add('fast-safe-stringify: large deep circular getters ', function () {
  114. fastSafeStringify(deepCircGetters)
  115. })
  116. suite.add('fast-safe-stringify: deep non-conf circular getters', function () {
  117. fastSafeStringify(deepCircNonCongifurableGetters)
  118. })
  119. // add listeners
  120. suite.on('cycle', function (event) {
  121. console.log(String(event.target))
  122. })
  123. suite.on('complete', function () {
  124. console.log('\nFastest is ' + this.filter('fastest').map('name'))
  125. })
  126. suite.run({ delay: 1, minSamples: 150 })