Alas, also has mis-use. You don't want to linearly parse urls, as a router! Addition was controversial because folks anticipated mis-use like this.
https://news.ycombinator.com/item?id=46043318
It would take a very large number of routes before linear search would become a noticeable performance problem.
At that point, you’d probably be splitting the router itself into multiple client bundles, with something at the root to quickly match the URL with a bundle of routes (maybe a hash table on the first URL segment, or even a trie).
This URLPattern library and linear search would still be a reasonable choice for implementing each individual route bundle. And in practice, just do it the naive way until it actually becomes a problem.
Can you talk more about this… I was under the impression that was the EXPLICIT reason [1] why it was added in the first place or did I misread your comment?
I just tried to match a URL against about a hundred patterns of various types (thanks to Claude code), expecting it to be a non-issue.
A hundred regex tests, for example, is generally very fast. A quick Python script made them run in 0.85ms. A hundred Flask router tests is 2.64ms.
So I had no reason to think this API would be slow. Surely matching a URL is a subset of generalized regexes and can only be fast? And given that routing is not an activity you do a lot, why would it matter anyway?
But the performances were atrocious: it took 8 seconds to resolve the worst-case scenario on Firefox, and it locked the entire browser UI.
Ok, note to self, stay away from the URL Pattern API.
Overall I dislike the shift away from a URL as a language-agnostic string primitive to some weird convoluted object which is limited to specific use cases.
URL literally stands for Universal Resource Locator... A string is Universal. It can be passed around easily between processes, it can be easily stored in a database, it can be easily shared online, it can be easily passed to an LLM... URLs were supported by LLMs before LLMs even existed! You've got to appreciate that!
This class they call URL is actually not a URL at all, it's more like a bound URLParser or URLExtractor.
A URL is a string that's a fact. Even ask Google; "is a URL a string?" it will say yes.
The idea of a URL instance as a language-specific construct is a bad idea. It's one of the reasons why many people don't like Java.
In Node.js, they deprecated perfectly adequate url parsing functions in favor of this clunky URL instance more recently.
It kind of reminds me of a more general trend of how modern operating systems like OSX and Windows try to hide the file system and current path of files... You can't even select the current path in your file explorer nowadays. It's why I use Linux.
Had this idea of a URLPattern been presented years ago alongside the original URL instance proposal, people would have discarded the entire concept... It's like, you're building up this elaborate object only to end up treating it like a string anyway. Making everyone's code more complicated than necessary. Moving away from simple, well understood primitives to language-specific neuroticism.
Great tool. So glad we have something!
Alas, also has mis-use. You don't want to linearly parse urls, as a router! Addition was controversial because folks anticipated mis-use like this. https://news.ycombinator.com/item?id=46043318
It would take a very large number of routes before linear search would become a noticeable performance problem.
At that point, you’d probably be splitting the router itself into multiple client bundles, with something at the root to quickly match the URL with a bundle of routes (maybe a hash table on the first URL segment, or even a trie).
This URLPattern library and linear search would still be a reasonable choice for implementing each individual route bundle. And in practice, just do it the naive way until it actually becomes a problem.
Can you talk more about this… I was under the impression that was the EXPLICIT reason [1] why it was added in the first place or did I misread your comment?
It’s also something the Lit team uses like here: https://www.npmjs.com/package/@lit-labs/router
I think maybe we are just debating the data structure the hold the patterns? Like it should be a trie rather than say a Set or Map.
[1] https://developer.chrome.com/docs/web-platform/urlpattern
I just tried to match a URL against about a hundred patterns of various types (thanks to Claude code), expecting it to be a non-issue.
A hundred regex tests, for example, is generally very fast. A quick Python script made them run in 0.85ms. A hundred Flask router tests is 2.64ms.
So I had no reason to think this API would be slow. Surely matching a URL is a subset of generalized regexes and can only be fast? And given that routing is not an activity you do a lot, why would it matter anyway?
But the performances were atrocious: it took 8 seconds to resolve the worst-case scenario on Firefox, and it locked the entire browser UI.
Ok, note to self, stay away from the URL Pattern API.
...Eight seconds for a hundred matches? What does your code look like?
> Note: This feature is available in Web Workers.
... is _also_ available in Web Workers, or _only_ available in Web Workers?
Also
I don't like this API.
Overall I dislike the shift away from a URL as a language-agnostic string primitive to some weird convoluted object which is limited to specific use cases.
URL literally stands for Universal Resource Locator... A string is Universal. It can be passed around easily between processes, it can be easily stored in a database, it can be easily shared online, it can be easily passed to an LLM... URLs were supported by LLMs before LLMs even existed! You've got to appreciate that!
This class they call URL is actually not a URL at all, it's more like a bound URLParser or URLExtractor.
A URL is a string that's a fact. Even ask Google; "is a URL a string?" it will say yes.
The idea of a URL instance as a language-specific construct is a bad idea. It's one of the reasons why many people don't like Java.
The class isn’t called URL, it’s called URLPattern. Because it represents a pattern that URLs can be matched against.
I'm aware. This is the continuation of an existing slow-creeping over-engineering trend.
First it was https://developer.mozilla.org/en-US/docs/Web/API/URL
In Node.js, they deprecated perfectly adequate url parsing functions in favor of this clunky URL instance more recently.
It kind of reminds me of a more general trend of how modern operating systems like OSX and Windows try to hide the file system and current path of files... You can't even select the current path in your file explorer nowadays. It's why I use Linux.
Had this idea of a URLPattern been presented years ago alongside the original URL instance proposal, people would have discarded the entire concept... It's like, you're building up this elaborate object only to end up treating it like a string anyway. Making everyone's code more complicated than necessary. Moving away from simple, well understood primitives to language-specific neuroticism.