NanoID - Alternative To UUID
Tags: react, javascript, node, webdev
Published: 2021-05-23 16:15:46

Hey coders 👋

If you are a JavaScript developer, I am pretty much sure that you have used the npm package [`UUID`](https://www.npmjs.com/package/uuid) at least once in your development journey.

> For those who don't know what is `UUID`. It is an npm package to generate a unique string ID. Similar packages are available in other languages too.

But in this article I am not going to discuss `UUID`, rather I will discuss another awesome npm package to generate a unique ID known as [`NanoID`](https://www.npmjs.com/package/nanoid).

### What is NanoID?

A tiny, secure, URL-friendly, unique string ID generator for JavaScript.

### Why NanoID?

- It is smaller in size as it has no dependencies.
- It is 60% faster than UUID.
- It uses cryptographically strong random APIs.
- It uses a larger alphabet than UUID (A-Za-z0-9_-).

> We can control the behaviour of alphabets to be used.

NanoID is available in almost all the most used [programming languages](https://www.npmjs.com/package/nanoid#other-programming-languages).

`Disclaimer: All the above claims are picked from the package's docs itself. 😉`

### Implementation

It is very easy to implement. Will write the code in a Node.js environment using CommonJS import.

#### Basic Way

It will generate the ID synchronously.

```javascript
// Importing
const { nanoid } = require("nanoid");

// It will generate and return an ID with 21 characters
const id = nanoid();
```

#### Async way

It will generate the ID asynchronously.

```javascript
// Importing async API
const { nanoid } = require("nanoid/async");

// It will generate and return an ID with 21 characters
const id = await nanoid();
```

#### Custom Size

You can also pass the required ID's size as an argument.

```javascript
// Importing
const { nanoid } = require("nanoid");

// It will generate and return an ID with 10 characters
const id = nanoid(10);
```
Reducing the size will increase the collisions probability.

#### Non-Secure

If you want performance and not concerned with security, then you can use the non-secure way.

```javascript
// Importing non-secure API
const { nanoid } = require("nanoid/non-secure");

const id = nanoid();
```

#### Custom Character or Size

You can control what characters you want to be included in your ID.

```javascript
// Importing customAlphabet API
const { customAlphabet } = require("nanoid");

// First Param: Characters
// Second Param: ID size
const nanoid = customAlphabet("123456789qwerty", 8);

// Generated ID would be like: "q15y6e9r"
const id = nanoid();
```

You can also use the `customAlphabet` with `async way` and `non-secure way`.

```javascript
// Importing async API
const { customAlphabet} = require("nanoid/async");

// Importing non-secure API
const { customAlphabet} = require("nanoid/non-secure");
```

You can also check for the ID collision probability [here](https://zelark.github.io/nano-id-cc/).

---

Originally published at [blog.bibekkakati.me](https://blog.bibekkakati.me/nanoid-alternative-to-uuid)

---

Thank you for reading 🙏

If you enjoyed this article or found it helpful, give it a thumbs-up 👍

Feel free to connect 👋

[Twitter](https://twitter.com/kakatibibek) | [Instagram](https://instagram.com/bibekkakati) | [LinkedIn](https://linkedin.com/in/bibekkakati)

---

If you like my work and want to support it, you can do it here. I will really appreciate it.

<a href="https://www.buymeacoffee.com/bibekkakati" target="_blank">
<img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=bibekkakati&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff">
</a>