benchmark.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { Bench } from 'tinybench'
  2. import { fastUri } from '../index.js'
  3. import { parse as uriJsParse, serialize as uriJsSerialize, resolve as uriJsResolve, equal as uriJsEqual } from 'uri-js'
  4. const base = 'uri://a/b/c/d;p?q'
  5. const domain = 'https://example.com/foo#bar$fiz'
  6. const ipv4 = '//10.10.10.10'
  7. const ipv6 = '//[2001:db8::7]'
  8. const urn = 'urn:foo:a123,456'
  9. const urnuuid = 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
  10. const urnuuidComponent = {
  11. scheme: 'urn',
  12. nid: 'uuid',
  13. uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
  14. }
  15. const {
  16. parse: fastUriParse,
  17. serialize: fastUriSerialize,
  18. resolve: fastUriResolve,
  19. equal: fastUriEqual,
  20. } = fastUri
  21. // Initialization as there is a lot to parse at first
  22. // eg: regexes
  23. fastUriParse(domain)
  24. uriJsParse(domain)
  25. const benchFastUri = new Bench({ name: 'fast-uri benchmark' })
  26. const benchUriJs = new Bench({ name: 'uri-js benchmark' })
  27. const benchWHATWG = new Bench({ name: 'WHATWG URL benchmark' })
  28. benchFastUri.add('fast-uri: parse domain', function () {
  29. fastUriParse(domain)
  30. })
  31. benchUriJs.add('urijs: parse domain', function () {
  32. uriJsParse(domain)
  33. })
  34. benchWHATWG.add('WHATWG URL: parse domain', function () {
  35. // eslint-disable-next-line
  36. new URL(domain)
  37. })
  38. benchFastUri.add('fast-uri: parse IPv4', function () {
  39. fastUriParse(ipv4)
  40. })
  41. benchUriJs.add('urijs: parse IPv4', function () {
  42. uriJsParse(ipv4)
  43. })
  44. benchFastUri.add('fast-uri: parse IPv6', function () {
  45. fastUriParse(ipv6)
  46. })
  47. benchUriJs.add('urijs: parse IPv6', function () {
  48. uriJsParse(ipv6)
  49. })
  50. benchFastUri.add('fast-uri: parse URN', function () {
  51. fastUriParse(urn)
  52. })
  53. benchUriJs.add('urijs: parse URN', function () {
  54. uriJsParse(urn)
  55. })
  56. benchWHATWG.add('WHATWG URL: parse URN', function () {
  57. // eslint-disable-next-line
  58. new URL(urn)
  59. })
  60. benchFastUri.add('fast-uri: parse URN uuid', function () {
  61. fastUriParse(urnuuid)
  62. })
  63. benchUriJs.add('urijs: parse URN uuid', function () {
  64. uriJsParse(urnuuid)
  65. })
  66. benchFastUri.add('fast-uri: serialize URN uuid', function () {
  67. fastUriSerialize(urnuuidComponent)
  68. })
  69. benchUriJs.add('uri-js: serialize URN uuid', function () {
  70. uriJsSerialize(urnuuidComponent)
  71. })
  72. benchFastUri.add('fast-uri: serialize uri', function () {
  73. fastUriSerialize({
  74. scheme: 'uri',
  75. userinfo: 'foo:bar',
  76. host: 'example.com',
  77. port: 1,
  78. path: 'path',
  79. query: 'query',
  80. fragment: 'fragment'
  81. })
  82. })
  83. benchUriJs.add('urijs: serialize uri', function () {
  84. uriJsSerialize({
  85. scheme: 'uri',
  86. userinfo: 'foo:bar',
  87. host: 'example.com',
  88. port: 1,
  89. path: 'path',
  90. query: 'query',
  91. fragment: 'fragment'
  92. })
  93. })
  94. benchFastUri.add('fast-uri: serialize long uri with dots', function () {
  95. fastUriSerialize({
  96. scheme: 'uri',
  97. userinfo: 'foo:bar',
  98. host: 'example.com',
  99. port: 1,
  100. path: './a/./b/c/../.././d/../e/f/.././/',
  101. query: 'query',
  102. fragment: 'fragment'
  103. })
  104. })
  105. benchUriJs.add('urijs: serialize long uri with dots', function () {
  106. uriJsSerialize({
  107. scheme: 'uri',
  108. userinfo: 'foo:bar',
  109. host: 'example.com',
  110. port: 1,
  111. path: './a/./b/c/../.././d/../e/f/.././/',
  112. query: 'query',
  113. fragment: 'fragment'
  114. })
  115. })
  116. benchFastUri.add('fast-uri: serialize IPv6', function () {
  117. fastUriSerialize({ host: '2606:2800:220:1:248:1893:25c8:1946' })
  118. })
  119. benchUriJs.add('urijs: serialize IPv6', function () {
  120. uriJsSerialize({ host: '2606:2800:220:1:248:1893:25c8:1946' })
  121. })
  122. benchFastUri.add('fast-uri: serialize ws', function () {
  123. fastUriSerialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true })
  124. })
  125. benchUriJs.add('urijs: serialize ws', function () {
  126. uriJsSerialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true })
  127. })
  128. benchFastUri.add('fast-uri: resolve', function () {
  129. fastUriResolve(base, '../../../g')
  130. })
  131. benchUriJs.add('urijs: resolve', function () {
  132. uriJsResolve(base, '../../../g')
  133. })
  134. benchFastUri.add('fast-uri: equal', function () {
  135. fastUriEqual('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d')
  136. })
  137. benchUriJs.add('urijs: equal', function () {
  138. uriJsEqual('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d')
  139. })
  140. await benchFastUri.run()
  141. console.log(benchFastUri.name)
  142. console.table(benchFastUri.table())
  143. await benchUriJs.run()
  144. console.log(benchUriJs.name)
  145. console.table(benchUriJs.table())
  146. await benchWHATWG.run()
  147. console.log(benchWHATWG.name)
  148. console.table(benchWHATWG.table())