benchmark.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict'
  2. const benchmark = require('benchmark')
  3. const suite = new benchmark.Suite()
  4. const fasturi = require('./')
  5. const urijs = require('uri-js')
  6. const base = 'uri://a/b/c/d;p?q'
  7. const domain = 'https://example.com/foo#bar$fiz'
  8. const ipv4 = '//10.10.10.10'
  9. const ipv6 = '//[2001:db8::7]'
  10. const urn = 'urn:foo:a123,456'
  11. const urnuuid = 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
  12. // Initialization as there is a lot to parse at first
  13. // eg: regexes
  14. fasturi.parse(domain)
  15. urijs.parse(domain)
  16. suite.add('fast-uri: parse domain', function () {
  17. fasturi.parse(domain)
  18. })
  19. suite.add('urijs: parse domain', function () {
  20. urijs.parse(domain)
  21. })
  22. suite.add('WHATWG URL: parse domain', function () {
  23. // eslint-disable-next-line
  24. new URL(domain)
  25. })
  26. suite.add('fast-uri: parse IPv4', function () {
  27. fasturi.parse(ipv4)
  28. })
  29. suite.add('urijs: parse IPv4', function () {
  30. urijs.parse(ipv4)
  31. })
  32. suite.add('fast-uri: parse IPv6', function () {
  33. fasturi.parse(ipv6)
  34. })
  35. suite.add('urijs: parse IPv6', function () {
  36. urijs.parse(ipv6)
  37. })
  38. suite.add('fast-uri: parse URN', function () {
  39. fasturi.parse(urn)
  40. })
  41. suite.add('urijs: parse URN', function () {
  42. urijs.parse(urn)
  43. })
  44. suite.add('WHATWG URL: parse URN', function () {
  45. // eslint-disable-next-line
  46. new URL(urn)
  47. })
  48. suite.add('fast-uri: parse URN uuid', function () {
  49. fasturi.parse(urnuuid)
  50. })
  51. suite.add('urijs: parse URN uuid', function () {
  52. urijs.parse(urnuuid)
  53. })
  54. suite.add('fast-uri: serialize uri', function () {
  55. fasturi.serialize({
  56. scheme: 'uri',
  57. userinfo: 'foo:bar',
  58. host: 'example.com',
  59. port: 1,
  60. path: 'path',
  61. query: 'query',
  62. fragment: 'fragment'
  63. })
  64. })
  65. suite.add('urijs: serialize uri', function () {
  66. urijs.serialize({
  67. scheme: 'uri',
  68. userinfo: 'foo:bar',
  69. host: 'example.com',
  70. port: 1,
  71. path: 'path',
  72. query: 'query',
  73. fragment: 'fragment'
  74. })
  75. })
  76. suite.add('fast-uri: serialize IPv6', function () {
  77. fasturi.serialize({ host: '2606:2800:220:1:248:1893:25c8:1946' })
  78. })
  79. suite.add('urijs: serialize IPv6', function () {
  80. urijs.serialize({ host: '2606:2800:220:1:248:1893:25c8:1946' })
  81. })
  82. suite.add('fast-uri: serialize ws', function () {
  83. fasturi.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true })
  84. })
  85. suite.add('urijs: serialize ws', function () {
  86. urijs.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true })
  87. })
  88. suite.add('fast-uri: resolve', function () {
  89. fasturi.resolve(base, '../../../g')
  90. })
  91. suite.add('urijs: resolve', function () {
  92. urijs.resolve(base, '../../../g')
  93. })
  94. suite.on('cycle', cycle)
  95. suite.run()
  96. function cycle (e) {
  97. console.log(e.target.toString())
  98. }