NanoID is a lib for generating random identifiers. It is compared to UUID but said faster, smaller in code size, friendlier in URL, and shorter in length (21 symbols vs 36) although both have the same collision probability. Also NanoId is widely ported to over 20 programming languages.
For instance, with speed of generating 1000 IDs per hours, it needs ~149 billion years or 1,307,660T IDs in order to have a 1% probability of at least one collision.
Install
npm install nanoid
Usage
import { nanoid } from 'nanoid';
const id = nanoid(); // "V1StGXR8_Z5jdHi6B-myT"
In case you want to customize alphabet or ID length:
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('1234567890abcdef', 10);
const id = nanoid(); // "4f90d13a42"
References
- NanoID The offical github of NanoID project.
- NanoID Collision Calculator shows collision probability in visuals.