Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a unique string
const uid = () => {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
};
// Usage. Example, id = khhry2hb7uip12rj2iu
const id = uid();
@KMLX28

This comment has been minimized.

Copy link

@KMLX28 KMLX28 commented Nov 20, 2020

Better:

const uid = () => Date.now().toString(36) + Math.random().toString(36).substr(2);
@codeStryke

This comment has been minimized.

Copy link

@codeStryke codeStryke commented Nov 21, 2020

how about using performance.now() instead of Date.now()? It should reduce the probability of collision further.

@SimonHoiberg

This comment has been minimized.

Copy link
Owner Author

@SimonHoiberg SimonHoiberg commented Nov 22, 2020

Better:

const uid = () => Date.now().toString(36) + Math.random().toString(36).substr(2);

That's a matter of preference, but yeah 😊

how about using performance.now() instead of Date.now()? It should reduce the probability of collision further.

Even if you're creating thousands of IDs every second, the likelihood of collision is ridiculously low here 😅
But technically speaking, I guess you're right, yeah!

@Eugeno

This comment has been minimized.

Copy link

@Eugeno Eugeno commented Nov 22, 2020

Math.random() can behave deterministically: https://www.tomanthony.co.uk/blog/googlebot-javascript-random/
Prefer Crypto API

@SimonHoiberg

This comment has been minimized.

Copy link
Owner Author

@SimonHoiberg SimonHoiberg commented Nov 22, 2020

Math.random() can behave deterministically: https://www.tomanthony.co.uk/blog/googlebot-javascript-random/
Prefer Crypto API

Absolutely. This is well-known.
That's why it is combined with Date.now().

Let me also just state that this is great for smaller lists and for quickly generating temporary unique strings.
It shouldn't be used for more serious cases, where collisions should be taken into account.
In those cases, use uuid4 instead.

@andreasmischke

This comment has been minimized.

Copy link

@andreasmischke andreasmischke commented Nov 24, 2020

I'm still not sure if I like or dislike that the IDs' alphabetical sort order is also the order by creation time...

@depoulo

This comment has been minimized.

Copy link

@depoulo depoulo commented Nov 24, 2020

You guys sure know about this one? https://gist.github.com/jed/982883

@andreasmischke

This comment has been minimized.

Copy link

@andreasmischke andreasmischke commented Nov 24, 2020

You guys sure know about this one? https://gist.github.com/jed/982883

Sure, but this here is still nice for smaller use cases and 87% faster on my machine: https://jsbench.me/ovkhvq1uct/1

@fernandocanizo

This comment has been minimized.

Copy link

@fernandocanizo fernandocanizo commented Nov 25, 2020

how about using performance.now() instead of Date.now()? It should reduce the probability of collision further.

It will only work on the web, not under Nodejs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.