Created
April 4, 2025 09:52
-
-
Save mizchi/78779075e9b82914c3b80885880cb631 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`); |
Author
mizchi
commented
Apr 4, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment