top of page

Introduction to URL-Friendly NanoID

NanoID

It is a tiny, secure, URL-friendly, unique string ID generator.

  • Nano ID has a smaller size as compared to UUID. This size reduction impacts a lot. Making use of NanoID is easier for transferring information and storage space. In a large-scale system, these numbers can make a lot of difference.

  • NanoID uses a cryptographically strong API which is a lot safer compared to Math.Random() which are insecure. These API modules use unpredictable hardware-generated random identifiers.

  • NanoID utilizes its very own “uniform formula” throughout the application of the ID generator instead of making use of an arbitrary % alphabet which is a popular mistake to make when coding an ID generator (The spread will not be even in some cases).

  • NanoID uses a larger alphabet resulting in short but unique identifiers.

  • NanoID permits designers to utilize personalized alphabets. This is another additional function of Nano ID. You can alter the literals or the dimension of the id as shown below (Specifying personalized letter as ‘1234567890ABCDEF’ & dimension of the Id as 10):

import { alphabet } from 'nanoid'; 
const nanoid = alphabet ('1234567890ABCDEF', 10); 
model.id = nanoid();
  • NanoID doesn’t much rely on any kind of third-party dependencies, which means, it ends up being a lot more steady which is helpful to maximize the package scope over time as well as make it much less vulnerable to the issues that come along with dependencies.

  • NanoID is available in various programs languages, which include — C#, C++, Dart & Flutter, Go, Java, PHP, Python, Ruby, Rust, Swift, etc.


Example usages

Generating both NanoID or UUID is pretty straightforward. In JavaScript, you have NPM packages that will help you to generate them. You can get NanoId from here => github.com/ai/nanoid

  • The main module uses URL-friendly symbols (A-Za-z0–9_-) and returns an ID with 21 characters:

import { nanoid } from "nanoid";
model.id = nanoid() // X2JaSYP7_Q2leGI9b-MyA
  • You can also specify the number of characters you want:

nanoid(9); // "wMeKBp6th"
  • You can also change the used alphabet for generating hashes to your own if you have specific requirements as seen above:

const alphabet = '0123456789ABCDEF';
generate(alphabet, 9); // F65BF3050


Features:

  1. NanoID is 4.5 times smaller in size and does not have any dependencies. The size limit is used to reduce the size from another 35%

  2. NanoID uses crypto_module and web_crypto_API which make it more secure.

  3. Instead of 36 characters it has only 21 characters which makes it 60% faster.

  4. NanoID supports 14 different programming languages : C#, C++, Crystal, Dart, Flutter, Deno, Go, Java, PHP, Elixir, Haskell, Janet, Nim, Perl, python, Ruby, Rust and Swift.

  5. It also supports PouchDB, CouchDB Webworkers, Rollup, React and React-native.

  6. It allow developers to use custom alphabets. you can change the size of the ID.

  7. It is more stable self-governed.

  8. It can optimize bundle size in long run and make it less prone to issue that come with dependencies.


Disadvantages:

  1. NanoID is harder to debug.

  2. NanoID is not sequential due to which problem occurs when you use same column as a clustered index.



The Tech Platform

0 comments

Recent Posts

See All
bottom of page