Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
URL to key/value parsing
(version: 0)
Test of extracting key/value pairs from the location.href or other URLs
Comparing performance of:
Manual parsing vs Regex parsing 1
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getConfigFromUrl(url) { if(!url) url = location.href; var question = url.indexOf("?"); var hash = url.indexOf("#"); if(hash==-1 && question==-1) return {}; if(hash==-1) hash = url.length; var query = question==-1 || hash==question+1 ? url.substring(hash) : url.substring(question+1,hash); var result = {}; query.split("&").forEach(function(part) { if(!part) return; part = part.split("+").join(" "); // replace every + with space, regexp-free version var eq = part.indexOf("="); var key = eq>-1 ? part.substr(0,eq) : part; var val = eq>-1 ? decodeURIComponent(part.substr(eq+1)) : ""; var from = key.indexOf("["); if(from==-1) result[decodeURIComponent(key)] = val; else { var to = key.indexOf("]",from); var index = decodeURIComponent(key.substring(from+1,to)); key = decodeURIComponent(key.substring(0,from)); if(!result[key]) result[key] = []; if(!index) result[key].push(val); else result[key][index] = val; } }); return result["viewer1.angle"]; } function regexMatch(url) { const keys = url.match(/(?<=\?.*)[A-z]+[A-z0-9]*.[A-z]+[A-z][A-z0-9]*(?=&?)/g); const vals = url.match(/(?<=[A-z]+[A-z0-9]*.[A-z]+[A-z0-9]*=)[^&]+(?=&?)/g); let output = {}; for (let i=0; i<keys.length; i++) { output[keys[i]] = vals[i]; } return output["viewer1.angle"]; } let r;
Tests:
Manual parsing
r = getConfigFromUrl("http://localhost:8181/?viewer1.angle=123&viewer1.color=234234&viewer2.angle=123");
Regex parsing 1
r = regexMatch("http://localhost:8181/?viewer1.angle=123&viewer1.color=234234&viewer2.angle=123");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Manual parsing
Regex parsing 1
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):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons of each approach, library usage, special JavaScript features or syntax, and alternatives. **Benchmark Definition** The provided JSON defines a benchmark named "URL to key/value parsing". It tests two different approaches for extracting key-value pairs from URLs: 1. **Manual parsing**: This approach uses a custom function `getConfigFromUrl` that manually parses the URL to extract key-value pairs. 2. **Regex parsing**: This approach uses another custom function `regexMatch` that leverages regular expressions (regex) to parse the URL. **Options Compared** The benchmark compares the performance of these two approaches: * **Manual parsing**: Uses a custom, regex-free function to manually parse the URL. * **Regex parsing**: Uses a custom function with regex patterns to parse the URL. **Pros and Cons of Each Approach** * **Manual parsing**: + Pros: No external dependencies (e.g., no regex libraries), easier to understand for some users, can be optimized for specific use cases. + Cons: Can be slower and more prone to errors due to manual parsing logic. * **Regex parsing**: + Pros: Faster execution times, less prone to errors, leverages powerful pattern matching capabilities. + Cons: Requires a regex library (in this case, the `match` method), can be overkill for simple use cases. **Library Usage** The benchmark uses the following libraries: * **regexMatch**: A custom function that leverages the `match` method of JavaScript strings to parse URLs using regex patterns. + Purpose: Provides a concise way to extract key-value pairs from URLs using regex patterns. **Special JavaScript Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. However, it's worth noting that the use of `let` and arrow functions (`=>`) is modern JavaScript syntax. **Alternatives** Other approaches could be used for URL parsing, such as: * Using a dedicated URL parsing library like `urlparser` or `querystring`. * Leveraging a framework-specific API (e.g., React's `useParams` hook). * Utilizing a third-party service for URL parsing (e.g., an API endpoint). These alternatives might offer better performance, error handling, or maintainability, but they would require additional dependencies and modifications to the benchmark code. In summary, the benchmark tests two approaches for extracting key-value pairs from URLs: manual parsing using a custom function and regex parsing using another custom function. The pros and cons of each approach are discussed, along with library usage and special JavaScript features or syntax. Alternative approaches can be considered depending on specific requirements and preferences.
Related benchmarks:
Partial base, search, fragment of URL
Partial base, search, fragment of URL 2
Partial base, search, fragment of URL 3
Split vs new URL pathname
Comments
Confirm delete:
Do you really want to delete benchmark?