Skip to content

Commit 0ee9c34

Browse files
jasnelltargos
authored andcommitted
benchmark: add simple parse and test benchmarks for URLPattern
PR-URL: #56882 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 5c82ef3 commit 0ee9c34

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

benchmark/url/urlpattern-parse.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { URLPattern } = require('url');
4+
const { ok } = require('assert');
5+
6+
const tests = [
7+
'https://(sub.)?example(.com/)foo',
8+
{ 'hostname': 'xn--caf-dma.com' },
9+
{ 'pathname': '/foo', 'search': 'bar', 'hash': 'baz',
10+
'baseURL': 'https://example.com:8080' },
11+
{ 'pathname': '/([[a-z]--a])' },
12+
];
13+
14+
const bench = common.createBenchmark(main, {
15+
pattern: tests.map(JSON.stringify),
16+
n: [1e5],
17+
});
18+
19+
function main({ pattern, n }) {
20+
const inputPattern = JSON.parse(pattern);
21+
let deadcode;
22+
bench.start();
23+
for (let i = 0; i < n; i += 1) {
24+
deadcode = new URLPattern(inputPattern);
25+
}
26+
bench.end(n);
27+
ok(deadcode);
28+
}

benchmark/url/urlpattern-test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { URLPattern } = require('url');
4+
const { notStrictEqual } = require('assert');
5+
6+
const tests = [
7+
'https://(sub.)?example(.com/)foo',
8+
{ 'hostname': 'xn--caf-dma.com' },
9+
{ 'pathname': '/foo', 'search': 'bar', 'hash': 'baz',
10+
'baseURL': 'https://example.com:8080' },
11+
{ 'pathname': '/([[a-z]--a])' },
12+
];
13+
14+
const bench = common.createBenchmark(main, {
15+
pattern: tests.map(JSON.stringify),
16+
n: [1e5],
17+
});
18+
19+
function main({ pattern, n }) {
20+
const inputPattern = JSON.parse(pattern);
21+
const urlpattern = new URLPattern(inputPattern);
22+
23+
let deadcode;
24+
bench.start();
25+
for (let i = 0; i < n; i += 1) {
26+
deadcode = urlpattern.test('https://sub.example.com/foo');
27+
}
28+
bench.end(n);
29+
notStrictEqual(deadcode, undefined); // We don't care if it is true or false.
30+
}

0 commit comments

Comments
 (0)