DEV Community

loading...
Cover image for NanoID - Alternative To UUID

NanoID - Alternative To UUID

Bibek
Hey, 👋 I'm a full-stack developer with expertise in the MERN stack. I do freelancing, contract jobs for my living 👨🏼‍💻
Originally published at blog.bibekkakati.me ・2 min read

Hey coders 👋

If you are a JavaScript developer, I am pretty much sure that you have used the npm 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.

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.

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.

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

// It will generate and return an ID with 21 characters
const id = nanoid();
Enter fullscreen mode Exit fullscreen mode

Async way

It will generate the ID asynchronously.

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

// It will generate and return an ID with 21 characters
const id = await nanoid();
Enter fullscreen mode Exit fullscreen mode

Custom Size

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

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

// It will generate and return an ID with 10 characters
const id = nanoid(10);
Enter fullscreen mode Exit fullscreen mode

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.

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

const id = nanoid();
Enter fullscreen mode Exit fullscreen mode

Custom Character or Size

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

// 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();
Enter fullscreen mode Exit fullscreen mode

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

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

// Importing non-secure API
const { customAlphabet} = require("nanoid/non-secure");
Enter fullscreen mode Exit fullscreen mode

You can also check for the ID collision probability here.


Originally published at blog.bibekkakati.me


Thank you for reading 🙏

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

Feel free to connect 👋

Twitter | Instagram | LinkedIn


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



Discussion (21)

Collapse
pcjmfranken profile image
Peter Franken • Edited

FYI A UUID method was added to NodeJS' core crypto module in v15.6.0. So it's not available in the current LTS, but v16 takes over that role come October.

New method's docs: nodejs.org/api/crypto.html#crypto_...

Collapse
bibekkakati profile image
Bibek Author

Hey, thank you for sharing that.

Collapse
galkin profile image
Nikita Galkin

Hi, Bibek.

There is my benchmarking test crypto.randomUUID is three times faster uuid.v4. I hope you can use it as idea for your testings.

Collapse
ianwijma profile image
Ian Wijma

That's actually quite interesting. Thanks for sharing!

Collapse
bibekkakati profile image
Bibek Author

Hey Galkin, thank you for sharing that.

Collapse
newbeb profile image
Brian Lloyd-Newberry

I use nanoid for anywhere I want identifiers that can easily be used in uris, especially ones that humans see. I've a postgresql UDF implementation that I can post about.

Collapse
bibekkakati profile image
Bibek Author

Yeah. That would be great.

Collapse
adisreyaj profile image
Adithya Sreyaj

Have been using it for a while now. Great little library 🔥

Collapse
bibekkakati profile image
Bibek Author

Yup. Very useful and reliable.

Collapse
senthilmpro profile image
Senthil Muthuvel

It would be great to compare Node.js Crypto module's UUID vs NanoID performance.

Collapse
bibekkakati profile image
Bibek Author

Hey, thank you for the suggestion. I will definitely try to do that.

Collapse
pankajtanwarbanna profile image
Pankaj Tanwar

Good stuff. Would you mind elaborating more details of why exactly nanoID is faster then UUID? What architectural changes helped nanoID improve it's speed?

Collapse
bibekkakati profile image
Bibek Author

Hey, I haven't done any in-depth research on both the packages, it's just the practical implementation. But as per NanoID's documentation, they are managing the memory in a better way, which makes them faster than UUID. You can check their benchmarks or you can test it by yourself too.

Collapse
kalashin1 profile image
Kinanee Samson

hate generating user id's, we already have to cope with naming things

Collapse
bibekkakati profile image
Bibek Author

Haha :-D

Collapse
divyakshshukla profile image
Divyaksh

This is nice and I will definitely use it my custom blog / portfolio project

Collapse
bibekkakati profile image
Bibek Author

I'm glad you liked it.

Collapse
kitkatkitt profile image
kitkatkitt

Thanks for sharing this, it really helped me out!

Collapse
bibekkakati profile image
Bibek Author

Thank you for your comment.

Collapse
Sloan, the sloth mascot
Comment deleted
Collapse
jamartinez98 profile image
jamartinez98

test