UUID is a universal identifier, and one that is the most used for developing softwares. Recently it has been getting many competitors. From the competition UUID was the one that posed a real competition. In this article we will be taking our shot on the reason why NanoID is losing its market to UUID.
Understand the NanoID
Javascript experts generate a NanoID or UUID for uniquely identifying packages etc,. For that you install the NanoID NPM library in your system. You can either do it manually or use the NPM nanoID command.
Here is the command you can use:
import { nanoid } from 'nanoid';
model.id = nanoid();
Well, one of the reasons why developers are using UUID over NanoID is it is faster. On top of that, NanoID technology is new and compensates for the downsides UUID had. In Spite, being a newer technology it has more stars on GitHub compared to UUID.
The key difference between the two is how they use the alphabets to make the unique key. Because of using a larger alphabet collection, NanoID can have a smaller ID and serve the same purpose.
Difference in byte Size
The biggest difference is in the byte size of the two. NanoID has a size of 108 bytes and compared to the UUID it is 4.5 times smaller. On top of being small it also does not have any dependencies on others.
Smaller size has a direct affect on the size of data that NanoID produces. So, an object created using the NanoID will be about 35% smaller when compared to object size created using the UUID.
This reduces the size of applications, especially humongous ones, and with their growth it becomes more visible. This means the systems will not be stuck when using the software.
Security Features
When it comes to security, random number generators and functions for creating random numbers are not safe. On the other hand, NanoID creates numbers that are safe because they are created using a crypto module and Web Crypto API.
On top of that, an additional layer of security is added by calling NanoID’s own security algorithm which is implemented using the ID generated instead of creating random alphabets.
NanoID for Fast and Compact Output
Being smaller and creating smaller sized data means that NanoID is faster. This is another reason why Javascript experts prefer using NanoID. When compared, the results show that NanoID is 60% faster.
One of the reasons for it being fast is it works with only 21 characters.
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-
On top of that, it is already supporting 14 different programming languages, from Javascript to PHP, Java, Swift, to C++, C# and more.
Read more on NanoID vs UUID for detailed comparison and wise selection.
Compatibility
Talking about compatibility with other languages and databases, NanoID is compatible with a variety. Supporting everything from PouchDB, CouchDB WebWorkers, to libraries like React and Reach-Native, and many more compatibility performance.
With only installing NodeJS, you work around to get a unique ID by using the terminal. The redux toolkit that comes with you installing NodeJS can also be used to find the NanoID.
Here is the command you can use:
import { nanoid } from '@reduxjs/toolkit'
console.log(nanoid()) //'dgPXxUz_6fWIQBD8XmiSy'
Create your Custom Alphabets
To create your custom alphabets now you can use NanoID and get them done. JavaScript developers can create custom alphabets, here is an example of the code:
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('ABCDEF1234567890', 12);
model.id = nanoid();
The first parameter passed to the variable “nonoid” is the custom alphabet and the second parameter is its size.
No Third-Party Dependencies
Like mentioned above NanoID does not have any dependencies, this means no local and third-party dependencies. Thus, with time it will become more stable and you will have more governance. In easy terms, developers can optimize bundle size and make them avoid issues that dependencies bring.
Does it have any Limitations ?
According to developers and JavaScript experts NanoID does not have any significant disadvantages or limitations. Debugging is harder, which is seen as one of the disadvantages of using NanoID but it can be ignored when you are getting so many other features. When using NanoID for a table's primary key and using it as a clustered index you will definitely face a lot of problems.
Closing notes
NanoID is new and slowly overtaking UUID as the unique identifier and unique ID generator for developers and especially for JavaScript developers. The power of NanoID is, creating 2.2 million unique IDs per second is not a problem for it. If you use your custom alphabet combination it can create more than 1.8 million unique IDs per second.
When asked you can clearly see the winner is NanoID mainly because of the features like size, URL friendliness, security, and speed.
Top comments (0)