UUID vs Crypto.randomUUID vs NanoID

Abdel Matyne LAWANI
4 min readNov 18, 2021

--

Which is the best unique uuid generator that fits your project?

Having a unique identifier is an important requirement in many applications today. The universally unique identifiers are just 128-bit pieces of data, that are displayed as (128/4) = 32 hexadecimal digits. uuids address the problem of generating a unique ID to use for our data, either randomly or using some input data as seed.

However, ensuring uniqueness is a challenge in itself. How do you ensure that there is just one copy of the id? And even then make sure that there is no correlation between any two ids? This is a tradeoff between uniqueness and randomness, and something that the different uuid generators and versions solve in different ways.

UUID

For the creation of RFC4122 UUIDs, it has v1,v2..v5. uuid v1 is generated by using a combination of the host computer MAC address and the current date and time, so this guaranteed uniqueness comes at the cost of anonymity.

uuid v4 is generated randomly and with no inherent logic and with the sheer number of possible combinations (2¹²⁸), it would be almost impossible to generate a duplicate unless you are generating trillions of IDs every second, for many years but you should still add a uniqueness constraint to avoid v4 collision if your app is mission-critical.

uuid v5 unlike v1 or v4 is generated by providing two pieces of input information (input string and namespace) that are converted to a consistantuuid meaning any given combination of input and namespace will result in the same uuid, every time.

Features

  • Complete — Support for RFC4122 version 1, 3, 4, and 5 UUIDs;
  • Cross-platform;
  • Secure — Cryptographically-strong random values;
  • Small — Zero-dependency, small footprint;
  • Includes the uuid command line utility.

Quickstart

import { v4 as uuidv4 } from 'uuid';
let uuid = uuidv4();
console.log(uuid) // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

Crypto.randomUUID

Node.js API provides therandomUUID() method of the Crypto interface that allows generating random RFC 4122 Version 4 UUID strings.

Quickstart

let uuid = crypto.randomUUID();
console.log(uuid); // "36b8f84d-df4e-4d49-b662-bcde71a8764f"

Nano ID

Nano ID has 3 APIs: normal (blocking), asynchronous, and non-secure. By default, Nano ID uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID with 21 characters (to have a collision probability similar to UUID v4).

Features

  • Very small — 130 bytes (minified and gzipped);
  • No dependencies;
  • Fast — It is faster than other generators.
  • Safe — It uses hardware random generator. Can be used in clusters.
  • Short IDs — It uses a larger alphabet than UUID (A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols.
  • Portable. Nano ID was ported to 20 programming languages.

Quickstart

import { nanoid } from 'nanoid'
let uuid = nanoid();
console.log(uuid) // ⇨ "V1StGXR8_Z5jdHi6B-myT"

Nanoid and UUID on npm trends

Benchmarks

For benchmarking I prefer to use hyperfine. It is like apache benchmark, but for CLI commands. There are have three cases :

  1. require(‘uuid’).v4()
  2. require(‘crypto’).randomUUID()
  3. require(‘nanoid’).nanoid()

Let’s put them into three files:

// test-uuid-gen.js
const { v4 as uuidv4 } = require('uuid');

for (let i = 0; i < 10_000_000; i++) {
uuidv4();
}
// test-crypto-gen.js
const { randomUUID } = require('crypto');

for (let i = 0; i < 10_000_000; i++) {
randomUUID();
}
// test-nanoid-gen.js
const { nanoid } = require('nanoid');

for (let i = 0; i < 10_000_000; i++) {
nanoid();
}

Now we ready for benchmarking : hyperfine ‘node test-uuid-gen.js’ ‘node test-crypto-gen.js’ ‘node test-nanoid-gen.js’

Look at the command results, Awesome!

As we see, the crypto interface randomUUID() method is 4 times faster than nanoid and 12 times than uuid .

Conclusion

UUID is the most popular library for universal unique identifier generation. The statistics above will allow you to choose what is best suit for you but I recommend using randomUUID() for your next project 😉.

If you enjoyed this article or found it helpful, give it a thumbs-up 👍 and feel free to connect 👋 . All constructive feedback is welcome.

Thank you for reading 🙏

--

--

Abdel Matyne LAWANI

Software Engineer | Trying to use tech to solve problems | Gamer | Traveler and I raise my mug of coffee to you!