Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,167 members, 7,780,183 topics. Date: Thursday, 28 March 2024 at 10:36 AM

How Does Nanoid Work? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Does Nanoid Work? (219 Views)

How Does Airtime And Bills Payment Apis Work For Fintechs Integration? / How Does Banking Software Works? / How Does It Take To Learn Python programming language Easy And Faster? (2) (3) (4)

(1) (Reply)

How Does Nanoid Work? by ellakilfoyle749(m): 12:00pm On Mar 21, 2023
To identify objects uniquely from one another every system needs a unique ID. To create those unique IDs there are several methods. To use random number generators causes security risk. This is where unique identifiers come in, and NanoID is great at it. Even though UUID is one of the most used unique universal identifiers for JS, in this article we will be talking about NanoID. You will get to know what is NanoID and what can NanoID tech do.

NanoID is an alternative to UUID, it will fit your requirements which UUIDs can achieve, and actually perform better than UUID covering its shortcomings.

Introducing Nano ID

A secure and URL-friendly universal unique ID generator with very small size of 108 bytes, here are the features of NanoID

1. It uses more alphabet combinations which gives unique identifiers which are short
2. With smaller size, it helps lessen the impact on the process of creating unique IDs. The size matters in a large system where thousands of unique numbers are required to be created every second.
3. Instead of creating random numbers the NanoID has a system of creating cryptographically secure APIs. These are a lot safer when compared to the ones created with random number generator methods.
4. Instead of creating arbitrary and random combinations of alphabets NanoID works on its formula to create the ID. So, when you are creating unique IDs with NanoID make sure not to use arbitrary % alphabets.
5. Using and creating personalized alphabets is promoted with NanoID. Which means you can have more control over the alphabets and ID created from it. Here is an example code of how to create personal alphabet:
import { alphabet } from 'nanoid';
const nanoid = alphabet ('1234567890ABCDEF', 10);
model.id = nanoid();
6. The dependencies of NanoID on third-party products is next to none. Meaning you have a highly secure and steady unique ID generator which can help maximize package scope.
7. The support of various programming language and frameworks is great. Here are few to name that NanoID has support for:C#, C++, Dart & Flutter, Go, Java, PHP, Python, Ruby, Rust, Swift, etc.

Example Usages

First you can get the NanoID form this GitHub repo:github.com/ai/nanoid

1. Here the module is using URL-friendly symbols (A-Za-z0-9_-) and as a result you get an ID having 21 characters:

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

// specify the number of characters in ID
model.id = nanoid(11)

2. Talking about more control. You can tell the generator alphabets and number you want to use for creating the ID. here is a code sample:
const alphabet = '0123456789ABCDEF';
generate(alphabet, 9);

Repetition or Collision Risk

NanoID is capable of generating over 2.2 million unique IDs every second, having default alphabet settings. But, what is the chance of repetition?

“~149 billion years needed, in order to have a 1% probability of at least one collision.”
According to report 1 and report 2.

There is a probability of repetition happening but the probability of it happening in the same program is extremely small. So small that it does not matter.

Disadvantages of generating IDs with NanoID

The ID you will be creating with it is hard to read for humans. Which can be seen as a disadvantage for some, because you cannot understand just by reading it.

Get this example, you get a call from a customer and they want you to give them a unique ID. You run the NanoID and get a unique ID from it. Telling that ID to the customer will be weird and confusing over a call. This is difficult and a disadvantage when you compare the ID generated by UUID, because it is easy to read and does not confuse anyone.


The UUID can easily be used by the database table for its primary keys. But if you use NanoID for the primary keys in the database it will create problems. The primary reason being, NanoID is not sequential.

The nature of these uniquely created IDs is that they do not have a sequence and they are clustered indexes. For the database every database entry made the key should be maintained and a record could be accessed via this unique key. With such a long, clustered, and unorganized key getting data becomes next to impossible and time taking.

(1) (Reply)

Why Choose Qilksense Training In Chennai / Call Of Duty: Modern Warfare; Multi-player, Game Hacking For Free / I Am In Need Of An App Developer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.