Skip to content

Instantly share code, notes, and snippets.

@mikaelvesavuori
Last active December 18, 2023 09:12
Show Gist options
  • Save mikaelvesavuori/dcf0a29f7c2d89a6b408448132dcfba0 to your computer and use it in GitHub Desktop.
Save mikaelvesavuori/dcf0a29f7c2d89a6b408448132dcfba0 to your computer and use it in GitHub Desktop.
Different ways to output IDs with Node.
/**
* @see https://medium.com/geekculture/the-wild-world-of-unique-identifiers-uuid-ulid-etc-17cfb2a38fce
* @see https://www.honeybadger.io/blog/uuids-and-ulids/
*
* Install with `npm init -y && npm install uuid nanoid ulidx -S`
*/
import { randomUUID } from 'crypto';
import { v4 } from 'uuid';
import { nanoid } from 'nanoid';
import { ulid } from 'ulidx';
const id_1 = randomUUID();
console.log('Random UUID (native):', id_1);
// Random UUID (native): b148806d-61c6-4342-9695-8cd4b95271ef
const id_2 = v4();
console.log('UUID v4:', id_2);
// UUID v4: e0694bba-f38f-4c21-b068-883b1710fbf6
const id_3 = nanoid();
console.log('Nanoid:', id_3);
// Nanoid: dtoeX8xh5rCX-bh2qS9bQ
const id_4 = ulid();
console.log('ULID:', id_4);
// ULID: 01HHY2H8FNWTDWCFTJXJYHFM9F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment