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.
10 comments
[ 6.5 ms ] story [ 31.9 ms ] threadSample Ruby code:
It's less collision resistant than UUID, of course.https://github.com/turbo/pg-shortkey
https://github.com/lukeed/uid
https://hashids.org
You can also encode UUIDs in base32 instead of base16, which will make them about 20% smaller.
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.
In python:
[1] https://github.com/pyrige/skippy/blob/master/skippy/skippy.p...
[2] https://gist.githubusercontent.com/adyliu/4494223/raw/0e21a0...
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.
https://github.com/ai/nanoid
It has a nice calculator for collision probability as well.