Skip to content

Commit 3273aed

Browse files
committed
benchmark: add simple parse and test benchmarks for URLPattern
1 parent c21d576 commit 3273aed

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

benchmark/url/urlpattern-parse.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
bench.start();
22+
for (let i = 0; i < n; i += 1) {
23+
const p = new URLPattern(inputPattern);
24+
ok(p);
25+
}
26+
bench.end(n);
27+
}

benchmark/url/urlpattern-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
bench.start();
24+
for (let i = 0; i < n; i += 1) {
25+
const p = urlpattern.test('https://sub.example.com/foo');
26+
notStrictEqual(p, undefined); // We don't care if it is true or false.
27+
}
28+
bench.end(n);
29+
}

0 commit comments

Comments
 (0)