jauntywundrkind
15 hours ago
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
tshaddox
14 hours ago
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.
mdhb
15 hours ago
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
jauntywundrkind
8 hours ago
Hono for example has a RegExpRouter and a TrieRouter, both of which seek for a matching route from amid multiple options. URL Patterns, plural!
They also have a linear search router, which they even say could have wins in some cases. But for relatively complex apps, with lots of possible sub-routes, the idea of running theonesr search feels so bad to me.
BiteCode_dev
14 hours ago
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.
saghm
6 hours ago
For what it's worth, quite a lot of libraries don't use NFA/DFA style regexes and instead use something like PCRE, which aren't not necessarily linear in the worst case. I'd hope that URL pattern matching wouldn't need recursive backtracking or whatever, but probably quite a lot of the time people use libraries with the less performance implementations they're not intending to use those features either, so it probably wouldn't be the first time anyone accidentally make their matching way slower from this if that's what happened here.
creatonez
13 hours ago
...Eight seconds for a hundred matches? What does your code look like?
BiteCode_dev
12 hours ago
My bad, I should not read AI generated code while drunk at a xmas party. That's the total run time for 10000 iterations.
Average time for 100 tests is hence 0.8 ms. Completely normal, and absolutely acceptable, especially for an operation as rare as routing.
Letting my previous comment as-is for historical purposes. And to remind myself I'm a dumbass.
elcritch
11 hours ago
In the near future I fear there may be laws about “LLMing while drunk” after enough rogue LLM agents vibe coded while drunk cause widespread havoc. You know folks harassing exs or trying to hack military depos to get a tank.
Actually that’d be a fun sci-fi book.