UUID vs Crypto.randomUUID vs NanoID

UUID

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

Quickstart

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

Nano ID

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

  1. require(‘uuid’).v4()
  2. require(‘crypto’).randomUUID()
  3. require(‘nanoid’).nanoid()
// 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();
}

Conclusion

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Ransomware: Avoid becoming a victim in 5 easy steps

Push notifications a Win-Win for user and the entity.

How To Setup A Site To Site VPN Connection with Strongswan

Hedera Technical Insights:​ Maintaining a decentralized identity registry with Hedera

Interview with Dennys Antonialli on Internet Governance and the NETmundial experience

🔺Reminder! List of official DISCIPLINA token addresses🔺

{UPDATE} BUTTONSiO Hack Free Resources Generator

{UPDATE} Custom Time Splits Hack Free Resources Generator

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdel Matyne LAWANI

Abdel Matyne LAWANI

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

More from Medium

Comparative Web Performance evaluation & benchmarking of Vue & React using JSWFB

How to setup a Svelte with Typescript project

Memory inspection — Chrome vs. Firefox

How to debug Node with Typescript in Visual Studio Code( ts-node-dev)