4 ms·
Ask HN: IDs that are URL-friendly and fairly short?
I am looking for a way (or best practices) on fairly short IDs that are also URL friendly.
In a few past projects I've used UUID v4s with a custom encoding to shorten them down to 26 characters. But those IDs were still fairly long.
I found this topic surprisingly difficult to google for. Any pointers or opinions very welcome.
- zzo38computer 6y agoI think it will depend on the application. Some things will require longer IDs than others will use.
- phillipseamore 6y agohttps://zelark.github.io/nano-id-cc/ https://zelark.github.io/nano-id-cc/
- gurgeous 6y agoDepends how much randomness you need. I usually just use a rand alpha of length 6-8. You can also use dictionary words (see gfycat). Sample Ruby code: 8.times.map { (('a'..'z').to_a + (0..9).to_a).sample }.join It's less collision resistant than UUID, of course.
- blondin 6y agohere's a couple of bookmarked links: https://github.com/turbo/pg-shortkey https://github.com/turbo/pg-shortkey https://github.com/lukeed/uid https://github.com/lukeed/uid https://hashids.org https://hashids.org
- alexmingoia 6y agoAuto-incremented integers encoded in base32. You can also encode UUIDs in base32 instead of base16, which will make them about 20% smaller.
- davidajackson 6y agoThis answer is assuming you want to sacrifice collision resistance for length (seems dubious for anything that needs to be highly reliable) and that you don't want to use auto-incrementing. Assuming your UUIDs are sufficiently random, isn't this just a generalized birthday problem? Take the birthday problem and generalize the formula from 365 buckets to N buckets, where N is the total number of possible UUIDs given a length and set of possible characters. https://mste.illinois.edu/courses/mat764fa03/folders/edwardssu/Tech_Port/ThesisPape02.html https://mste.illinois.edu/courses/mat764fa03/folders/edwards... You should be concerned with what happens if there's a duplicate -- you can calculate the probability of that occurring as long as you know the potential size of your dataset and set your UUID length accordingly.
- asplake 6y agoIf 32 bit keys are enough, skip32 [1] and then alphanumeric encoding [2] on an incrementing integer key. Both of those are reversible so you won't suffer from collisions and you can get back your original integer key if you want. In python: [1] https://github.com/pyrige/skippy/blob/master/skippy/skippy.py https://github.com/pyrige/skippy/blob/master/skippy/skippy.p... [2] https://gist.githubusercontent.com/adyliu/4494223/raw/0e21a012ba312776f41c15fb378cee5e1357bf70/base62.py https://gist.githubusercontent.com/adyliu/4494223/raw/0e21a0...
- deleted 6y ago[deleted]
- Foober223 6y agoDo the values need to be secret? Not guessable/predictable? If that's not a concern, just increment a number 1, 2, 3...N. Encode it in the URL in base-36 (alpha numeric). It all depends on the requirements of your ID.
- tdeck 6y agoI have been using NanoID in JS, and it works well. It also has a variant that will check for rude words, in case the identifier is particularly user-visible. https://github.com/ai/nanoid https://github.com/ai/nanoid It has a nice calculator for collision probability as well.