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
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]>
  • Loading branch information
jasnell authored Feb 4, 2025
1 parent 316ac8c commit 3207fda
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions benchmark/url/urlpattern-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'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);
let deadcode;
bench.start();
for (let i = 0; i < n; i += 1) {
deadcode = new URLPattern(inputPattern);
}
bench.end(n);
ok(deadcode);
}
30 changes: 30 additions & 0 deletions benchmark/url/urlpattern-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'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);

let deadcode;
bench.start();
for (let i = 0; i < n; i += 1) {
deadcode = urlpattern.test('https://sub.example.com/foo');
}
bench.end(n);
notStrictEqual(deadcode, undefined); // We don't care if it is true or false.
}

0 comments on commit 3207fda

Please sign in to comment.