DEV Community

Discussion on: Generate a unique random code to store in DB

Collapse
 
antondachauer profile image
Anton Dachauer

That's nice, but maybe it would be making sense to add a functionality to "auto-choose" the length of the code. At first, you can start with "0", then "1" ... "a0" ... "ax0" and so on. So, the code will start faster using it and with the time it's going to be slower. When the length of the code is round 10 signs, there was have already billions of unique codes generated.

Collapse
 
mmj profile image
MMJ

No a bad idea at all. Could be useful when you don't have to stick to a fixed lentgh for other reasons.
Can't think about downsides about your suggestion offhand.
Maybe it will work faster than with fixed lenght at the beginning, with few records, but slower at certain point?

Collapse
 
antondachauer profile image
Anton Dachauer

I don't know, which variant is faster overall. Depending on the possibilities and the technical infrastructure, it makes sense to store the used keys in a mongodb, which would be faster in storing key-value-pairs. Or elasticsearch or any other NoSQL-Databse.