Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/scripts/bench-parse-url-relation-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { parseDomain, fromUrl, NO_HOSTNAME } from "../main.js";
// Extracted from https://github.com/stevenvachon/url-relation/blob/main/test/helpers/tests.json
// (all `url1`/`url2` values across all test cases, duplicates included).
import urls from "./fixtures/url-relation-test-urls.json" with { type: "json" };

// Warm up: the trie is parsed lazily on the first parseDomain() call and then
// memoized, so exclude that one-time cost from the measured loop below.
parseDomain("example.com");

const start = performance.now();

for (const url of urls) {
const hostname = fromUrl(url);

if (hostname !== NO_HOSTNAME) {
parseDomain(hostname);
}
}

const end = performance.now();
const durationMs = end - start;

console.log(`Parsed ${urls.length} URLs in ${durationMs.toFixed(2)}ms`);
console.log(`Average: ${(durationMs / urls.length).toFixed(4)}ms per URL`);
53 changes: 53 additions & 0 deletions src/scripts/bench-parse-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { parseDomain, fromUrl, NO_HOSTNAME } from "../main.js";

const TLDS = [
"com",
"org",
"net",
"io",
"dev",
"co.uk",
"de",
"app",
"info",
"shop",
"cloud",
"xyz",
];
const SUBDOMAINS = ["", "www.", "api.", "shop.blog.", "cdn.assets.", "mail."];

const generateUrls = (count: number): Array<string> => {
const urls: Array<string> = [];

for (let i = 0; i < count; i++) {
const tld = TLDS[i % TLDS.length];
const subdomain = SUBDOMAINS[i % SUBDOMAINS.length];

urls.push(`https://${subdomain}example-${i}.${tld}/path/${i}?query=${i}`);
}

return urls;
};

const URL_COUNT = 23_104;
const urls = generateUrls(URL_COUNT);

// Warm up: the trie is parsed lazily on the first parseDomain() call and then
// memoized, so exclude that one-time cost from the measured loop below.
parseDomain("example.com");

const start = performance.now();

for (const url of urls) {
const hostname = fromUrl(url);

if (hostname !== NO_HOSTNAME) {
parseDomain(hostname);
}
}

const end = performance.now();
const durationMs = end - start;

console.log(`Parsed ${URL_COUNT} URLs in ${durationMs.toFixed(2)}ms`);
console.log(`Average: ${(durationMs / URL_COUNT).toFixed(4)}ms per URL`);
Loading
Loading