How to Generate Unique ID in JS without any Library?

In many cases, we need to generate unique numbers or ID

But, you may think, is my ID really unique? 😕



Here, we will see how you can generate unique ID that can be "safely" used as "unique". 


What do you mean by unique?

Remember, if you are thinking unique ID means no other ID in the world will match with yours, that's kinda wrong. Our main goal is to create an ID that's unique within your app.

By the way, sometimes, it can be unique in the whooooole world 😏



How to add the Uniqueness? 

Instead of using only numbers, we will add some letters as well. 

We will use timestamp in milliseconds. But, it can't ensure total uniqueness. 

So, we will multiply it with a random number generated by Math.random()

To increase "uniqueness", we generate a word with some random characters and finally mix them together and shuffle. 😎

The Code

👇 Check out the code which follows the idea above -

let timestamp = +new Date
let rand = parseInt(Math.random() * 1e5)

let finalNumb = timestamp * rand

let string = (Math.random() + 1).toString(36).substring(7)
let mixedID = finalNumb + string + '-'

const shuffle = str => [...str].sort(()=>Math.random()-.5).join('')

const finalID = shuffle(mixedID)

console.log(finalID) 
//generates an ID like: 462120g56n566s-13160f23v


I added a little twist here 😋, putting a hyphen in the id. You can use any other character or even just avoid it.

You can limit the final id to a specific length if needed.


🏹 Performance


How fast? 🚀

I executed around 5000 tests and the time taken for generating a single ID was the following -
  • Minimum time: 0.1 ms
  • Maximum time: 2.6 ms

The most popular unique id generation library is UUID. It can generate an ID in less than 0.05 ms. So, obviously, this method is slower than UUID.

How Unique? 👾

Timestamp in milliseconds is not totally unique. There is possibility that same timestamp will be generated multiple times.

This method uses Math.random() 3 times. You may know this gives you a number from 2^53 options. So, there is obviously possibility that same number will be generated in future.

But, we have combined those 4 "random" number together. Is there any possibility that all these will repeat?

Although, theoritically it's possible but the chance is too low to happen in real world. So, you can safely use this in your application.


Should I use it? 🤔

It depends on you. The javascript uuid library is really good and simple. So, I don't see a reason why you should not use that.

But, you can obviously use this method, maybe updating and optimizing it in your way. You can create a function for this and use it wherever needed. It's not as fast as uuid but can still generate max 10k unique id per second and that should be enough.

😌 After all, take this as fun and a piece of project to learn. 

👀 Conclusion




I hope, I was able to show you a way to generate unique ID with plain JS without any library.

If you have any suggestion, please let me know in the comments below. 👇

Thanks for reading!
















Comments