Skip to content

Commit

Permalink
benchmark: 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 3, 2025
1 parent 2bd5694 commit 2919b1e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
27 changes: 27 additions & 0 deletions benchmark/url/urlpattern-parse.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 { ok } = require('assert');

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) {
const p = new URLPattern(inputPattern);
ok(p);
}
bench.end(n);
}
29 changes: 29 additions & 0 deletions benchmark/url/urlpattern-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common.js');
const { URLPattern } = require('url');
const { notStrictEqual } = require('assert');

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) {
const p = urlpattern.test('https://sub.example.com/foo');
notStrictEqual(p, undefined); // we don't care if it is true or false.

Check failure on line 26 in benchmark/url/urlpattern-test.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Comments should not begin with a lowercase character
}
bench.end(n);
}

0 comments on commit 2919b1e

Please sign in to comment.