Debian Codegraph @

fast-uri/benchmark/benchmark.mjs

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