NanoID: Smaller and More Powerful Identifier, Perfect For Javascript

Bo Wanvisa Eamsiri
2 min readJul 1, 2021

--

As we all know, a universally unique identifier (UUID) is a label used for identifying information needed to be unique within a system, software, or hardware. They are meant to be unique and rarely repeated, suitable for being made as keys used in things like databases or hardware identification.

Although UUID is one of the most used identifiers, its library does take up some space, and there are also specific dependencies involved. There are some other identifiers that are great alternatives to UUIDs and have other benefits to offer. One of them that I’ll talk about today is NanoID.

NanoID is a unique string generator for Javascript coming in a highly tiny package.

Why should we use NanoID?

  1. It is cryptographically solid and safe. Thanks to its unpredictability from using the crypto module in Node.js and the Web Crypto API in browsers.
  2. It is minimal. It only has 108 bytes, which is 4.5 times smaller than UUID.
  3. It is super fast. (Due to its memory allocation tricks). It is 60% faster than UUID!
  4. It requires zero dependencies.
  5. It can be ported with almost every programming language.

Implementation:

Install it with this command

npm i nanoid

There are many ways to use it. Here are the examples:

1. The simple way

const { nanoid } = require(“nanoid”);

const id = nanoid();

2. Asynchronous way

const { nanoid } = require(“nanoid/async”);

const id = await nanoid();

3. With customized size (regular size is 21 characters), in this example, the size is 16

const { nanoid } = require(“nanoid”);

const id = nanoid(16);

In conclusion, NanoID is another identifier that should be implemented more. It has made its way at the top of the list of UUID competitors due to it being a smaller, more portable, and faster alternative.

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

--

--