benchmark.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 deep = require('./package.json')
  11. deep.deep = JSON.parse(JSON.stringify(deep))
  12. deep.deep.deep = JSON.parse(JSON.stringify(deep))
  13. deep.deep.deep.deep = JSON.parse(JSON.stringify(deep))
  14. deep.array = array
  15. const deepCirc = JSON.parse(JSON.stringify(deep))
  16. deepCirc.deep.deep.deep.circ = deepCirc
  17. deepCirc.deep.deep.circ = deepCirc
  18. deepCirc.deep.circ = deepCirc
  19. deepCirc.array = array
  20. suite.add('util.inspect: simple object', function () {
  21. inspect(obj)
  22. })
  23. suite.add('util.inspect: circular ', function () {
  24. inspect(circ)
  25. })
  26. suite.add('util.inspect: deep ', function () {
  27. inspect(deep)
  28. })
  29. suite.add('util.inspect: deep circular', function () {
  30. inspect(deepCirc)
  31. })
  32. suite.add('\njson-stringify-safe: simple object', function () {
  33. jsonStringifySafe(obj)
  34. })
  35. suite.add('json-stringify-safe: circular ', function () {
  36. jsonStringifySafe(circ)
  37. })
  38. suite.add('json-stringify-safe: deep ', function () {
  39. jsonStringifySafe(deep)
  40. })
  41. suite.add('json-stringify-safe: deep circular', function () {
  42. jsonStringifySafe(deepCirc)
  43. })
  44. suite.add('\nfast-safe-stringify: simple object', function () {
  45. fastSafeStringify(obj)
  46. })
  47. suite.add('fast-safe-stringify: circular ', function () {
  48. fastSafeStringify(circ)
  49. })
  50. suite.add('fast-safe-stringify: deep ', function () {
  51. fastSafeStringify(deep)
  52. })
  53. suite.add('fast-safe-stringify: deep circular', function () {
  54. fastSafeStringify(deepCirc)
  55. })
  56. // add listeners
  57. suite.on('cycle', function (event) {
  58. console.log(String(event.target))
  59. })
  60. suite.on('complete', function () {
  61. console.log('\nFastest is ' + this.filter('fastest').map('name'))
  62. })
  63. suite.run({ delay: 1, minSamples: 150 })