Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created April 4, 2025 09:52
Show Gist options
  • Save mizchi/78779075e9b82914c3b80885880cb631 to your computer and use it in GitHub Desktop.
Save mizchi/78779075e9b82914c3b80885880cb631 to your computer and use it in GitHub Desktop.
const iterations = 1000000; // 試行回数
const urlString = "https://example.com/path/to/resource?query=param#fragment";
const domainToCheck = "https://example.com";
console.log(`Benchmarking with ${iterations} iterations...\n`);
// --- new URL() benchmark ---
console.log(`1. Parsing URL with new URL("${urlString}")`);
const startNewUrl = performance.now();
for (let i = 0; i < iterations; i++) {
new URL(urlString);
}
const endNewUrl = performance.now();
const durationMsNewUrl = endNewUrl - startNewUrl;
const durationSecNewUrl = durationMsNewUrl / 1000;
const opsPerSecNewUrl = iterations / durationSecNewUrl;
console.log(
` Completed in ${durationMsNewUrl.toFixed(
2
)} ms (${durationSecNewUrl.toFixed(4)} seconds).`
);
console.log(` Operations per second: ${opsPerSecNewUrl.toFixed(2)}\n`);
// --- startsWith() benchmark ---
console.log(`2. Checking domain with urlString.startsWith("${domainToCheck}")`);
const startStartsWith = performance.now();
for (let i = 0; i < iterations; i++) {
urlString.startsWith(domainToCheck);
}
const endStartsWith = performance.now();
const durationMsStartsWith = endStartsWith - startStartsWith;
const durationSecStartsWith = durationMsStartsWith / 1000;
const opsPerSecStartsWith = iterations / durationSecStartsWith;
console.log(
` Completed in ${durationMsStartsWith.toFixed(
2
)} ms (${durationSecStartsWith.toFixed(4)} seconds).`
);
console.log(` Operations per second: ${opsPerSecStartsWith.toFixed(2)}`);
@mizchi
Copy link
Author

mizchi commented Apr 4, 2025

Benchmarking with 1000000 iterations...

1. Parsing URL with new URL("https://example.com/path/to/resource?query=param#fragment")
  Completed in 191.30 ms (0.1913 seconds).
  Operations per second: 5227434.27

2. Checking domain with urlString.startsWith("https://example.com")
  Completed in 23.78 ms (0.0238 seconds).
  Operations per second: 42047476.65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment