How to Generate UUIDs in JavaScript: Modern Browser API and Node.js
Learn how to use native crypto.randomUUID() in modern browsers and Node.js without heavy npm packages. Explore benchmarks and compatibility.
Modern browsers and Node.js (v14.17+) include native support for crypto.randomUUID(), generating cryptographically secure RFC 4122 v4 UUIDs in C++ without third-party npm packages like 'uuid'. It executes ~4x faster than userland JavaScript libraries and requires zero bundle overhead.
Solve this problem now with our free UUID / GUID Generator
Try it instantly in your browser — zero tracking, 100% client-side privacy.
Specification & Architecture Reference
| Approach | Dependencies | Ops / Sec (V8 Benchmark) | Bundle Footprint |
|---|---|---|---|
| crypto.randomUUID() | Native Web Crypto API | ~5,200,000 ops/sec | |
| npm 'uuid' package (v4) | External dependency | ~1,400,000 ops/sec | |
| Math.random() regex hack | Zero dependencies | ~850,000 ops/sec |
The Modern Native API: `crypto.randomUUID()`
For years, generating a UUID in JavaScript required installing the external `uuid` npm package. Today, the **Web Cryptography API** provides native UUID generation across all modern JavaScript runtimes.
// Native in modern browsers, Node.js 16.7+, Bun, and Deno:
const id = crypto.randomUUID();
console.log(id);
// Outputs: "c9a646d3-9c61-4cd7-893e-8cba475a6350"Browser & Runtime Support - **Chrome / Edge:** Version 92+ - **Safari:** Version 15.4+ - **Firefox:** Version 95+ - **Node.js:** Version 14.17.0+ (global `crypto` available in 19.0+)
Important Security Context: HTTPS Required In browser environments, `window.crypto.randomUUID()` is only accessible in **secure contexts** (pages served over `https://` or `localhost`). Calling it on an insecure plain HTTP origin results in: ```text TypeError: crypto.randomUUID is not a function ```
Safe Cross-Platform Fallback If your code needs to support older legacy environments or non-secure HTTP contexts:
function safeRandomUUID() {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
bytes[6] = (bytes[6] & 0x0f) | 0x40; // Version 4
bytes[8] = (bytes[8] & 0x3f) | 0x80; // Variant 10
return Array.from(bytes, (b, i) =>
([4, 6, 8, 10].includes(i) ? '-' : '') + b.toString(16).padStart(2, '0')
).join('');
}
throw new Error("No cryptographically secure random source available.");
}Generate secure UUIDs or test batch generation speeds directly in your browser with Mickytool's **UUID / GUID Generator**.
Solve this problem now with our free UUID / GUID Generator
Execute this workflow immediately without saving data on remote servers.
Frequently Asked Questions
Can I generate UUID v7 using native crypto.randomUUID()?
No. The native crypto.randomUUID() API is standardized specifically for version 4 (random) UUIDs. Generating UUID v7 currently requires a lightweight userland library.
Is crypto.randomUUID() synchronous or asynchronous?
It is completely synchronous and non-blocking, making it suitable for high-frequency loops.