• Replacing UUID: Why is The NanoID Replacing UUID?
  • Charuka Herath
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Hoarfroster
  • Reviewer:

UUID is one of the most commonly used universal identifiers in software development. In the past few years, however, other competitors have challenged its existence.

Among them, NanoID is one of the main competitors of UUID.

Therefore, in this article, we’ll explore NanoID’s capabilities, its highlights, and its limitations to give us a better idea of when to use it.


Learn about NanoID and its usage

For JavaScript, generating a UUID or NanoID is very simple. They all have a corresponding NPM package to help us with the generation.

All we need to do is run the NPM I nanoid command to install the Nanoid NPM library and use it in our project:

import { nanoid } from 'nanoid';  
model.id = nanoid();
Copy the code

Did you know that NanoID has over 11,754,000 NPM downloads per week and is running 60% faster than UUID?

In addition, NanoID is nearly seven years younger than the UUID and already has more GitHub stars than the UUID.

The figure below shows a comparison of NPM trends between the two, where we can see a sharp contrast between the rising trend of NanoID and the flat progress of UUID.

www.npmtrends.com/nanoid-vs-u…

I hope these numbers have convinced you to try NanoID.

But the main difference between the two is simple. It comes down to the alphabet the keys use.

Because NanoID uses a larger alphabet than UUID, the shorter ID can be used for the same purpose as the longer UUID.

1. NanoID is only 108 bytes long

Unlike UUID, NanoID is 4.5 times smaller and has no dependencies. In addition, size limits have been used to reduce the size from another 35%.

Size reduction directly affects the size of the data. For example, objects using NanoID are small and compact, and can be used for data transfer and storage. As the application grows, these numbers become apparent.

2. More safe

In most random generators, they use the unsafe math.random (). However, NanoID uses the Crypto Module and the Web Crypto API, which means NanoID is more secure.

In addition, NanoID uses its own algorithm in the implementation of the ID generator, called the unified algorithm, instead of using the “random % alphabet” random % alphabet.

3. It’s fast and compact

NanoID is 60% faster than UUID. Unlike the 36 characters in the UUID alphabet, NanoID has only 21 characters.

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-
Copy the code

In addition, NanoID supports 14 different programming languages:

C#, C++, Clojure and ClojureScript, Crystal, Dart & Flutter, Deno, Go, Elixir, Haskell, Janet, Java, Nim, Perl, PHP, with dictionary Python, Ruby, Rust, SwiftCopy the code

4. The compatibility

It also supports PouchDB, CouchDB WebWorkers, Rollup, and React and Reach-native libraries.

We can use NPX nanoid to get a unique ID in the terminal. The only requirement to use NanoID in JavaScript is to have NodeJS installed first.

In addition, NanoID can be found in the Redux Toolkit and used in other use cases, as shown below;

import { nanoid } from'@ reduxjs/toolkitconsole.log(nanoid()) / / 'dgPXxUz_6fWIQBD8XmiSy'
Copy the code

5. Customize letters

Another existing feature of NanoID is that it allows developers to use a custom alphabet. We can change the size of the text or ID as follows:

import { customAlphabet } from 'nanoid';  
const nanoid = customAlphabet('ABCDEF1234567890'.12);  
model.id = nanoid();
Copy the code

In the example above, I define the custom alphabet as ABCDEF1234567890 and the size of the Id as 12.

6. No third party dependencies

Because NanoID does not rely on any third party dependencies, it is able to become more stable and autonomous over time.

In the long run, this helps optimize package size and makes it less prone to dependency problems.


Limitations and future priorities

According to many experts in StackOverflow, there are no obvious drawbacks or limitations to using NanoID.

Non-human readable is the main drawback that many developers see in NanoID, because it makes debugging more difficult. However, compared to UUID, NanoID is shorter and readable.

Also, if you use NanoID as the primary key of the table, you will have problems if you use the same column as the clustered index. This is because the NanoID is not continuous.

In the future…

NanoID is becoming the most popular unique ID generator for JavaScript, and most developers prefer it to UUID.

Source:www.npmjs.com/package/nan…

The benchmark above shows NanoID’s performance compared to other major ID generators.

More than 2.2 million unique ids can be generated per second using the default alphabet and more than 1.8 million unique ids per second using the custom alphabet.

Based on my experience with UUID and NanoID, GIVEN its small size, URL-friendliness, security, and speed, I recommend using Nanoids instead of UUID in any future projects.

So I invite you to try out NanoID in your next project and share your thoughts with others in the comments section.

Thanks for reading!!

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.