a blog for those who code

Tuesday, 6 July 2021

Why we should use NanoID instead of UUID

UUID is one of the most used universal identifiers in software development. Though it's widely used, it has some flaws and over the years many alternatives came to minimize that flaws. NanoID is one of the alternatives which seems to be promising among many others. In this article, we will discuss NanoID and see all the features it has.


Generating NanoID or UUID seems pretty straightforward. In JavaScript, you have NPM packages that will help you to generate them. To generate NanoID you can use NanoID NPM Library and use the below code to get the id.


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


Some of the features which makes NanoID suitable than UUID are:


1. NanoID is only 108 bytes in size, and thus it makes your data smaller in size if you have the bulk of data.

2. Because of the smaller size, NanoID is 60% faster than UUID. 

3. Since NanoID uses cryptographically strong APIs instead of normal Math.random(), it is a little more secure than the UUIDs.

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

4. We can use custom alphabets with NanoID which is not possible with UUID, also NanoID does not depend on any third party and thus it seems to be more stable.

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

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

So if you want to try something else than UUID to reduce the size you can definitely try NanoID.

No comments:

Post a Comment