Need to create unique IDs that won't collide even across distributed systems? UUIDs (Universally Unique Identifiers) are your answer. In this notebook, we'll explore how to generate and work with UUIDs in JavaScript, the different versions available, and when to use each one.
Let's dive into the world of unique identifiers and see how they can solve common problems in web development, databases, and distributed systems.
$ npm install uuid added 1 package in 2s 29 packages are looking for funding run `npm fund` for details
UUIDs (also known as GUIDs - Globally Unique Identifiers) are 128-bit identifiers that are designed to be unique across space and time. They look like this: 123e4567-e89b-12d3-a456-426614174000
.
The chance of generating duplicate UUIDs is so astronomically low that for practical purposes, you can consider them unique. How low? About 1 in 5.3×10^36 (that's a 1 followed by 36 zeros) - way more atoms than exist on Earth!
Just generated a shiny new UUID: 3d98e665-05e6-43e1-843a-7efadcdb8c3a
There are multiple UUID versions, each with different generation mechanisms and use cases. The most common ones are v1, v4, and v5.
Version 4 UUIDs are generated using random numbers. These are the most common type and are perfect when you need something unpredictable with no embedded information.
UUID v4 (random): b443e34d-4fa4-4a1c-9708-7d8513a4aa70 Random UUID #1: f03fb726-c21c-4d7d-b708-34ec53546288 Random UUID #2: 8000398c-a188-43d7-837c-b0004b581f8c Random UUID #3: 27705e8f-6b47-438b-ace1-e94a105b23ce
UUID v1 combines the current timestamp with the MAC address of the computer. This makes them sortable by creation time, which can be handy for certain database operations.
UUID v1 (time-based): 19b7c8e0-00de-11f0-8323-87dc846d27c0 Another v1 UUID: 19b7eff0-00de-11f0-8323-87dc846d27c0 And another v1 UUID: 19b7eff1-00de-11f0-8323-87dc846d27c0
Version 5 UUIDs are generated from a namespace and a name. Given the same inputs, they'll always generate the same UUID - perfect for creating IDs from existing data.
UUID v5 for https://example.com: 4fd35a71-71ef-5a55-a9d9-aa75c889a6d0 UUID v5 for https://another-example.com: 2ce95335-6940-5fdb-a369-07ee5a25bbe2 Regenerated UUID for https://example.com: 4fd35a71-71ef-5a55-a9d9-aa75c889a6d0 Are they the same? true
The uuid package provides functionality to check if a string is a valid UUID and to determine which version it is.
Is "7bb63bfb-e73b-4a2b-b476-c321bccf029e" a valid UUID? true Is "definitely-not-a-uuid" a valid UUID? false The version of 7bb63bfb-e73b-4a2b-b476-c321bccf029e is: 4 Is our UUID a valid v4? true
The uuid package includes some special predefined UUIDs.
NIL UUID (all zeros): 00000000-0000-0000-0000-000000000000 Is it the NIL UUID? true
Let's build a simple user management system using UUIDs for different purposes:
Created user: { "id": "49ff1bd3-c83e-4740-989f-f43c24606676", "email": "alice@example.com", "name": "Alice", "createdAt": "2025-03-14T14:10:39.066Z" } Created session: { "id": "1b6292b0-00de-11f0-8323-87dc846d27c0", "userId": "49ff1bd3-c83e-4740-989f-f43c24606676", "createdAt": "2025-03-14T14:10:39.067Z", "expiresAt": "2025-03-14T15:10:39.067Z" } Verification token for alice@example.com: c65f4bbf-6d5a-5455-93ef-debfb4e07c98 Generated again: c65f4bbf-6d5a-5455-93ef-debfb4e07c98 Same token? true
Here's a quick comparison of UUIDs vs. auto-increment IDs for databases:
Simulating 5 distributed inserts... Generated 5 IDs Unique IDs: 5 Collisions: 0 Sample UUID: 900ff63b-2a05-4fb5-a02d-b4802c9ae228
Let's look at the performance and storage implications of UUIDs:
UUIDs aren't always the right choice. Here are some guidelines on when to use them:
✅ Use UUIDs when:
❌ Consider alternatives when:
UUIDs might seem like overkill for small projects, but they give you tremendous flexibility as your application grows! Think of them as future-proofing your ID system!