Generate UUID in JavaScript: 4 Methods Compared
How to generate UUIDs in JavaScript: crypto.randomUUID(), uuid npm package, custom implementation, and performance comparison.
4 Methods
// 1. Native (modern browsers + Node 19+)\ncrypto.randomUUID()\n\n// 2. uuid npm package\nimport { v4 as uuidv4 } from 'uuid';\nuuidv4()\n\n// 3. Custom (no dependencies)\nfunction uuid() {\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(\n /[xy]/g, c => {\n const r = crypto.getRandomValues(new Uint8Array(1))[0] & 15;\n return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);\n });\n}
Try It Free
Use our free online tool — 100% client-side, no data leaves your browser.
Open UUID Generator