Why is NanoID Replacing UUID?

A Tiny, URL-Friendly Unique String Generator for JavaScript.

UUID is one of the most used universal identifiers in software development. However, over the past few years, other alternatives challenged its existence.

Out of these, NanoID is one of the leading competitors to take over from UUIDs.

So, in this article, I will discuss the features of NanoID, where it shines, and its limitations to give you a better understanding of when to use it.

Understand NanoID and Its Usage

When it comes to JavaScript, generating either UUID or NanoID is pretty straightforward. They both have NPM packages to help you with it.

All you need to do is install the NanoID NPM library using npm i nanoid command and use it in your project.

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

Do you know that NanoID has over 11,754K weekly NPM downloads and 60% faster than UUID?

Besides, NanoID is almost 7 years younger than UUID, and it already has more GitHub stars than UUID.

The below graph shows the npm trends comparison between these 2, and we can see an upward trend of NanoID compared to the flat progress of UUID.

https://www.npmtrends.com/nanoid-vs-uuid

I hope these numbers have already convinced you to try out NanoID.

However, the main difference between these two is simple. It boils down to the alphabet used by the key.

Since NanoID uses a larger alphabet than UUID, a shorter ID can serve the same purpose as a longer UUID.

1. NanoID is Only 108 bytes in Size

Unlike UUID, NanoID is 4.5 times smaller in size and does not have any dependencies. Furthermore, the size limit has been used to reduce the size from another 35%.

The size reduction directly affects on size of the data. For instance, an object using NanoID is small and compact for data transfer and storage. With the application growth, these numbers become visible.

2. More Secure

In most of the random generators, they use unsafe Math.random(). But, NanoID uses crypto moduleand Web Crypto API which is more secure.

Also, NanoID has used its own algorithm called a uniform algorithm during the implementation of the ID generator instead of using a random % alphabet

3. It is Fast and Compact

NanoID is 60% faster than the UUID. Instead of having 36 characters in UUID’s alphabet, NanoID only has 21 characters.

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-

Also, NanoID support 14 different programming languages, which are,

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

4. Compatibility

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

You can get a unique ID in the terminal by using npx nanoid. The only prerequisite is to have NodeJS installed.

Besides, you can also find NanoID inside the Redux toolkit and use it for other use cases as follows;

import { nanoid } from ‘@reduxjs/toolkit’
console.log(nanoid()) //‘dgPXxUz_6fWIQBD8XmiSy’

5. Custom Alphabets

Another existing feature of NanoID is that it allows developers to use custom alphabets. You can change the literals or the size of the id as below:

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

In the above example, I have defined a custom Alphabet as ABCDEF1234567890 and the size of the Id as 12.

6. No Third-Party Dependencies

Since NanoID doesn’t depend on any third-party dependencies, over time, it becomes more stable self-governed.

This is beneficial to optimize the bundle size in the long run and make it less prone to the issues that come along with dependencies.

Limitations and Future Focus

Based on many expert opinions in StackOverflow, there are no significant disadvantages or limitations of using NanoID.

Being non-human-readable is the main disadvantage many developers see in the NanoID since it makes debugging harder. But, when compared to UUID, NanoID is way shorter and readable.

Also, if you use NanoID as a table’s primary key, there will be problems if you use the same column as a clustered index. This is because NanoIDs are not sequential.

In the future…

NanoID is gradually becoming the most popular unique id generator for JavaScript and most developers prefer to choose it over UUID.

Source: https://www.npmjs.com/package/nanoid

The above benchmarks show the performance of NanoID compared to other major id generators.

It can generate over 2.2 million unique IDs per second with its default alphabet and over 1.8 million unique IDs per second when using the custom alphabet.

With my experience in using both UUID and NanoID, I suggest using NanoID over UUID for any future projects considering its small size, URL friendliness, security, and speed.

So, I invite you to try out NanoID in your next project and share your thought with others in the comment section.

Thank you for Reading !!!

Build & share independent components with Bit

Bit is an ultra-extensible tool that lets you create truly modular applications with independently authored, versioned, and maintained components.

Use it to build modular apps & design systems, author and deliver micro frontends, or simply share components between applications.

An independently source-controlled and shared “card” component (on the right, its dependency graph, auto-generated by Bit)

Bit: The platform for the modular web

Read More


Why is NanoID Replacing UUID? was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Charuka Herath

A Tiny, URL-Friendly Unique String Generator for JavaScript.

UUID is one of the most used universal identifiers in software development. However, over the past few years, other alternatives challenged its existence.

Out of these, NanoID is one of the leading competitors to take over from UUIDs.

So, in this article, I will discuss the features of NanoID, where it shines, and its limitations to give you a better understanding of when to use it.

Understand NanoID and Its Usage

When it comes to JavaScript, generating either UUID or NanoID is pretty straightforward. They both have NPM packages to help you with it.

All you need to do is install the NanoID NPM library using npm i nanoid command and use it in your project.

import { nanoid } from 'nanoid';
model.id = nanoid();
Do you know that NanoID has over 11,754K weekly NPM downloads and 60% faster than UUID?

Besides, NanoID is almost 7 years younger than UUID, and it already has more GitHub stars than UUID.

The below graph shows the npm trends comparison between these 2, and we can see an upward trend of NanoID compared to the flat progress of UUID.

https://www.npmtrends.com/nanoid-vs-uuid

I hope these numbers have already convinced you to try out NanoID.

However, the main difference between these two is simple. It boils down to the alphabet used by the key.

Since NanoID uses a larger alphabet than UUID, a shorter ID can serve the same purpose as a longer UUID.

1. NanoID is Only 108 bytes in Size

Unlike UUID, NanoID is 4.5 times smaller in size and does not have any dependencies. Furthermore, the size limit has been used to reduce the size from another 35%.

The size reduction directly affects on size of the data. For instance, an object using NanoID is small and compact for data transfer and storage. With the application growth, these numbers become visible.

2. More Secure

In most of the random generators, they use unsafe Math.random(). But, NanoID uses crypto moduleand Web Crypto API which is more secure.

Also, NanoID has used its own algorithm called a uniform algorithm during the implementation of the ID generator instead of using a random % alphabet

3. It is Fast and Compact

NanoID is 60% faster than the UUID. Instead of having 36 characters in UUID’s alphabet, NanoID only has 21 characters.

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-

Also, NanoID support 14 different programming languages, which are,

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

4. Compatibility

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

You can get a unique ID in the terminal by using npx nanoid. The only prerequisite is to have NodeJS installed.

Besides, you can also find NanoID inside the Redux toolkit and use it for other use cases as follows;

import { nanoid } from ‘@reduxjs/toolkit’
console.log(nanoid()) //‘dgPXxUz_6fWIQBD8XmiSy’

5. Custom Alphabets

Another existing feature of NanoID is that it allows developers to use custom alphabets. You can change the literals or the size of the id as below:

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

In the above example, I have defined a custom Alphabet as ABCDEF1234567890 and the size of the Id as 12.

6. No Third-Party Dependencies

Since NanoID doesn’t depend on any third-party dependencies, over time, it becomes more stable self-governed.

This is beneficial to optimize the bundle size in the long run and make it less prone to the issues that come along with dependencies.

Limitations and Future Focus

Based on many expert opinions in StackOverflow, there are no significant disadvantages or limitations of using NanoID.

Being non-human-readable is the main disadvantage many developers see in the NanoID since it makes debugging harder. But, when compared to UUID, NanoID is way shorter and readable.

Also, if you use NanoID as a table’s primary key, there will be problems if you use the same column as a clustered index. This is because NanoIDs are not sequential.

In the future…

NanoID is gradually becoming the most popular unique id generator for JavaScript and most developers prefer to choose it over UUID.

Source: https://www.npmjs.com/package/nanoid

The above benchmarks show the performance of NanoID compared to other major id generators.

It can generate over 2.2 million unique IDs per second with its default alphabet and over 1.8 million unique IDs per second when using the custom alphabet.

With my experience in using both UUID and NanoID, I suggest using NanoID over UUID for any future projects considering its small size, URL friendliness, security, and speed.

So, I invite you to try out NanoID in your next project and share your thought with others in the comment section.

Thank you for Reading !!!

Build & share independent components with Bit

Bit is an ultra-extensible tool that lets you create truly modular applications with independently authored, versioned, and maintained components.

Use it to build modular apps & design systems, author and deliver micro frontends, or simply share components between applications.

An independently source-controlled and shared “card” component (on the right, its dependency graph, auto-generated by Bit)

Bit: The platform for the modular web

Read More


Why is NanoID Replacing UUID? was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Charuka Herath


Print Share Comment Cite Upload Translate Updates
APA

Charuka Herath | Sciencx (2021-06-29T16:41:41+00:00) Why is NanoID Replacing UUID?. Retrieved from https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/

MLA
" » Why is NanoID Replacing UUID?." Charuka Herath | Sciencx - Tuesday June 29, 2021, https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/
HARVARD
Charuka Herath | Sciencx Tuesday June 29, 2021 » Why is NanoID Replacing UUID?., viewed ,<https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/>
VANCOUVER
Charuka Herath | Sciencx - » Why is NanoID Replacing UUID?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/
CHICAGO
" » Why is NanoID Replacing UUID?." Charuka Herath | Sciencx - Accessed . https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/
IEEE
" » Why is NanoID Replacing UUID?." Charuka Herath | Sciencx [Online]. Available: https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/. [Accessed: ]
rf:citation
» Why is NanoID Replacing UUID? | Charuka Herath | Sciencx | https://www.scien.cx/2021/06/29/why-is-nanoid-replacing-uuid/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.