Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Partial base, search, fragment of URL 2
(version: 0)
Comparing performance of:
patern 1 vs patern 2 vs patern 3 vs patern 4
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var url1 = "https://localhost:3000/test?key1=value1&key2=value2#test"; const urlPattern = /^(https?:\/\/[^\/?#]+[^?#]*)?(?:\?([^#]*))?(?:#([^?]*))?(?:\?([^#]*))?/; function extractParts1(inputUrl) { const match = inputUrl.match(urlPattern); if (!match) return null; let url = match[1] || null; let searchParams = match[2] || null; let fragment = match[3] || null; // If there are query parameters after the fragment if (match[4]) { searchParams = searchParams ? searchParams + '&' + match[4] : match[4]; } return { url, searchParams, fragment }; } function extractParts2(inputUrl) { // Parse basic URL and fragment const [base, fragmentQuery] = inputUrl.split('#'); // Parse URL and search parameters from base let [url, searchParams] = base.split('?'); // If there's fragment data, check for additional query parameters let fragment = null; let extraSearchParams = null; if (fragmentQuery) { if (fragmentQuery.includes('?')) { [fragment, extraSearchParams] = fragmentQuery.split('?'); searchParams = searchParams ? `${searchParams}&${extraSearchParams}` : extraSearchParams; } else { fragment = fragmentQuery; } } return { url, searchParams: searchParams || null, fragment: fragment || null }; } function extractParts3(inputUrl) { const queryIndex = inputUrl.indexOf('?'); const hashIndex = inputUrl.indexOf('#'); const hasQueryBeforeHash = queryIndex !== -1 && (hashIndex === -1 || queryIndex < hashIndex); const url = hasQueryBeforeHash ? inputUrl.substring(0, queryIndex) : (hashIndex !== -1 ? inputUrl.substring(0, hashIndex) : inputUrl); let searchParams = null, fragment = null; if (hashIndex !== -1) { const fragmentWithQuery = inputUrl.substring(hashIndex + 1); const fragmentQueryIndex = fragmentWithQuery.indexOf('?'); if (fragmentQueryIndex !== -1) { fragment = fragmentWithQuery.substring(0, fragmentQueryIndex); searchParams = fragmentWithQuery.substring(fragmentQueryIndex + 1); if (hasQueryBeforeHash) { searchParams = inputUrl.substring(queryIndex + 1, hashIndex) + '&' + searchParams; } } else { fragment = fragmentWithQuery; } } if (hasQueryBeforeHash && !searchParams) { searchParams = hashIndex !== -1 ? inputUrl.substring(queryIndex + 1, hashIndex) : inputUrl.substring(queryIndex + 1); } return { url, searchParams: searchParams || null, fragment: fragment || null }; } function extractParts4(inputUrl) { return new URL(inputUrl); }
Tests:
patern 1
extractParts1(url1);
patern 2
extractParts2(url1);
patern 3
extractParts3(url1);
patern 4
extractParts4(url1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
patern 1
patern 2
patern 3
patern 4
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its various components. **Benchmark Definition** The benchmark is designed to measure the performance of four different approaches for extracting URL parts (url, search parameters, and fragment) from a given input URL. **Options Compared** The four options compared are: 1. `extractParts1(inputUrl)`: This function uses a regular expression to match the URL pattern and extract the individual parts. 2. `extractParts2(inputUrl)`: This function splits the URL into two parts: basic URL (base) and fragment query, and then parses the search parameters from the base part. 3. `extractParts3(inputUrl)`: This function uses a more complex approach to parse the URL, considering both query parameters and hash fragments. 4. `extractParts4(inputUrl)`: This function uses the built-in `URL` API to extract the individual parts of the URL. **Pros and Cons** Here's a brief summary of each option: 1. `extractParts1(inputUrl)`: * Pros: Simple, efficient, and well-supported. * Cons: May not work correctly for all edge cases (e.g., URLs with multiple fragments). 2. `extractParts2(inputUrl)`: * Pros: Easy to understand and implement, but may be slower due to string manipulation. * Cons: Less efficient than other options, especially for large inputs. 3. `extractParts3(inputUrl)`: * Pros: Handles more edge cases (e.g., URLs with multiple fragments), but is more complex. * Cons: May be slower and harder to maintain due to its complexity. 4. `extractParts4(inputUrl)`: * Pros: Fast, efficient, and well-supported, as it uses the built-in `URL` API. * Cons: Requires modern browsers or Node.js environment. **Library/Functionality** The `URL` API is used in `extractParts4(inputUrl)`. The `URL` interface provides a standardized way to parse URLs into their individual parts (protocol, hostname, pathname, search parameters, and hash). **Individual Test Cases** Each test case measures the performance of one specific option: 1. `patern 1`: `extractParts1(url1)` 2. `patern 2`: `extractParts2(url1)` 3. `patern 3`: `extractParts3(url1)` 4. `patern 4`: `extractParts4(url1)` **Benchmark Result** The latest benchmark result shows the performance metrics for each test case, including the number of executions per second. In summary, the benchmark compares four different approaches for extracting URL parts and measures their performance in modern browsers or Node.js environments. The results can help identify which approach is most efficient and reliable.
Related benchmarks:
URL params
Check URL protocol and domain new URL, includes, endWith vs Regex
Partial base, search, fragment of URL
Partial base, search, fragment of URL 3
Comments
Confirm delete:
Do you really want to delete benchmark?