Skip to content

Commit

Permalink
benchmarks: add simple parse and test benchmarks for URLPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Feb 2, 2025
1 parent 2bd5694 commit abd9d6b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
25 changes: 25 additions & 0 deletions benchmark/url/urlpattern-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const common = require('../common.js');
const { URLPattern } = require('url');

const tests = [
'https://(sub.)?example(.com/)foo',
{ 'hostname': 'xn--caf-dma.com' },
{ 'pathname': '/foo', 'search': 'bar', 'hash': 'baz',
'baseURL': 'https://example.com:8080' },
{ 'pathname': '/([[a-z]--a])' },
];

const bench = common.createBenchmark(main, {
pattern: tests.map(JSON.stringify),
n: [1e5],
});

function main({ pattern, n }) {
const inputPattern = JSON.parse(pattern);
bench.start();
for (let i = 0; i < n; i += 1) {
new URLPattern(inputPattern);
}
bench.end(n);
}
27 changes: 27 additions & 0 deletions benchmark/url/urlpattern-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common.js');
const { URLPattern } = require('url');

const tests = [
'https://(sub.)?example(.com/)foo',
{ 'hostname': 'xn--caf-dma.com' },
{ 'pathname': '/foo', 'search': 'bar', 'hash': 'baz',
'baseURL': 'https://example.com:8080' },
{ 'pathname': '/([[a-z]--a])' },
];

const bench = common.createBenchmark(main, {
pattern: tests.map(JSON.stringify),
n: [1e5],
});

function main({ pattern, n }) {
const inputPattern = JSON.parse(pattern);
const urlpattern = new URLPattern(inputPattern);

bench.start();
for (let i = 0; i < n; i += 1) {
urlpattern.test('https://sub.example.com/foo');
}
bench.end(n);
}

0 comments on commit abd9d6b

Please sign in to comment.