paralleldrive / cuid

Collision-resistant ids optimized for horizontal scaling and performance.
Other
3.44k stars 123 forks source link

slug() is generating 7 character IDs #16

Closed powmedia closed 9 years ago

powmedia commented 10 years ago

The documentation states that IDs generated through cuid.slug() weigh in at 8 characters, however the ones I'm seeing are 7 characters. Is this expected behaviour? Does the documentation mean they will be up to 8 characters?

ericelliott commented 9 years ago

Thanks for the heads up!

Fixed in https://github.com/ericelliott/cuid/commit/d6bd849d66befe8290fb794e98dfcc1176f0e733

ericelliott commented 9 years ago

Slugs are 7 characters. =)

powmedia commented 9 years ago

Having said that, one of the new slug IDs I've just seen created is 8 characters! On Nov 11, 2014 4:37 AM, "Eric Elliott" notifications@github.com wrote:

Slugs are 7 characters. =)

Reply to this email directly or view it on GitHub https://github.com/ericelliott/cuid/issues/16#issuecomment-62450623.

ericelliott commented 9 years ago

Oops. Is this causing problems? Maybe one of the entropy sources is just too short sometimes. I'd have to look at the implementation again and pad it for consistency.

powmedia commented 9 years ago

It's not causing problems right now but would be good to know it was consistent.

On Tue, Nov 11, 2014 at 12:29 PM, Eric Elliott notifications@github.com wrote:

Oops. Is this causing problems? Maybe one of the entropy sources is just too short sometimes. I'd have to look at the implementation again and pad it for consistency.

Reply to this email directly or view it on GitHub https://github.com/ericelliott/cuid/issues/16#issuecomment-62500674.

ericelliott commented 9 years ago

Length is not guaranteed.

MarkHerhold commented 8 years ago

@ericelliott Could you explain what part of the cuid slug may vary in length? I'm wondering if this would affect the alphanumeric sortability of slugs.

MarkHerhold commented 8 years ago

To answer my own question, the counter portion of the slug grows unboundedly, which is why there is no guarantee on length.

The 2-character date representation comes before the counter, so I'd consider the resulting records well sortable across instances when generating IDs every few milliseconds. For anyone reading this, please note that I am no statistician. :)

ericelliott commented 8 years ago

Want to submit a PR?

MarkHerhold commented 8 years ago

@ericelliott for what? I'm OK with the slug growing unbounded mostly because I don't have a better strategy for keeping everything short while maintaining a reasonable guarantee of uniqueness.

Or did you mean a docs update? :) I can do this tomorrow.

ericelliott commented 8 years ago

Docs would be fine. =)

MarkHerhold commented 7 years ago

I ran into this again and did re-investigated the code (I am running a SQL DB with fixed column lengths). It turns out that my previous conclusion that the slug grows unboundedly is incorrect. :confused:

The actual max slug length is 10 characters as the counter will only ever contribute a max of 4 characters to the slug length. Therefore, slugs range for 7 - 10 chars. I'll update the README. Sorry about this!