source: frontend/node_modules/fast-uri/benchmark/benchmark.mjs

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
1import { Bench } from 'tinybench'
2import { fastUri } from '../index.js'
3import { parse as uriJsParse, serialize as uriJsSerialize, resolve as uriJsResolve, equal as uriJsEqual } from 'uri-js'
4
5const base = 'uri://a/b/c/d;p?q'
6
7const domain = 'https://example.com/foo#bar$fiz'
8const ipv4 = '//10.10.10.10'
9const ipv6 = '//[2001:db8::7]'
10const urn = 'urn:foo:a123,456'
11const urnuuid = 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
12
13const urnuuidComponent = {
14 scheme: 'urn',
15 nid: 'uuid',
16 uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
17}
18
19const {
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
28fastUriParse(domain)
29uriJsParse(domain)
30
31const benchFastUri = new Bench({ name: 'fast-uri benchmark' })
32const benchUriJs = new Bench({ name: 'uri-js benchmark' })
33const benchWHATWG = new Bench({ name: 'WHATWG URL benchmark' })
34
35benchFastUri.add('fast-uri: parse domain', function () {
36 fastUriParse(domain)
37})
38benchUriJs.add('urijs: parse domain', function () {
39 uriJsParse(domain)
40})
41benchWHATWG.add('WHATWG URL: parse domain', function () {
42 // eslint-disable-next-line
43 new URL(domain)
44})
45benchFastUri.add('fast-uri: parse IPv4', function () {
46 fastUriParse(ipv4)
47})
48benchUriJs.add('urijs: parse IPv4', function () {
49 uriJsParse(ipv4)
50})
51benchFastUri.add('fast-uri: parse IPv6', function () {
52 fastUriParse(ipv6)
53})
54benchUriJs.add('urijs: parse IPv6', function () {
55 uriJsParse(ipv6)
56})
57benchFastUri.add('fast-uri: parse URN', function () {
58 fastUriParse(urn)
59})
60benchUriJs.add('urijs: parse URN', function () {
61 uriJsParse(urn)
62})
63benchWHATWG.add('WHATWG URL: parse URN', function () {
64 // eslint-disable-next-line
65 new URL(urn)
66})
67benchFastUri.add('fast-uri: parse URN uuid', function () {
68 fastUriParse(urnuuid)
69})
70benchUriJs.add('urijs: parse URN uuid', function () {
71 uriJsParse(urnuuid)
72})
73benchFastUri.add('fast-uri: serialize URN uuid', function () {
74 fastUriSerialize(urnuuidComponent)
75})
76benchUriJs.add('uri-js: serialize URN uuid', function () {
77 uriJsSerialize(urnuuidComponent)
78})
79benchFastUri.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})
90benchUriJs.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})
101benchFastUri.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})
112benchUriJs.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})
123benchFastUri.add('fast-uri: serialize IPv6', function () {
124 fastUriSerialize({ host: '2606:2800:220:1:248:1893:25c8:1946' })
125})
126benchUriJs.add('urijs: serialize IPv6', function () {
127 uriJsSerialize({ host: '2606:2800:220:1:248:1893:25c8:1946' })
128})
129benchFastUri.add('fast-uri: serialize ws', function () {
130 fastUriSerialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true })
131})
132benchUriJs.add('urijs: serialize ws', function () {
133 uriJsSerialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true })
134})
135benchFastUri.add('fast-uri: resolve', function () {
136 fastUriResolve(base, '../../../g')
137})
138benchUriJs.add('urijs: resolve', function () {
139 uriJsResolve(base, '../../../g')
140})
141
142benchFastUri.add('fast-uri: equal', function () {
143 fastUriEqual('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d')
144})
145benchUriJs.add('urijs: equal', function () {
146 uriJsEqual('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d')
147})
148
149await benchFastUri.run()
150console.log(benchFastUri.name)
151console.table(benchFastUri.table())
152
153await benchUriJs.run()
154console.log(benchUriJs.name)
155console.table(benchUriJs.table())
156
157await benchWHATWG.run()
158console.log(benchWHATWG.name)
159console.table(benchWHATWG.table())
Note: See TracBrowser for help on using the repository browser.