Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Create GUID / UUID in JavaScript?
A Globally Unique Identifier (GUID) or Universally Unique Identifier (UUID) is a 128-bit value used as a unique identifier in software development. It's typically represented as a 32-character hexadecimal string with hyphens, like: de305d84-75c4-431d-acc2-eb6b0e5f6014. Here are several JavaScript methods to generate GUIDs/UUIDs.
Using Random Number Generation
The most common approach uses Math.random() to generate a UUID v4 format:
function generate_uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {
var uuid = Math.random() * 16 | 0, v = c == 'x' ? uuid : (uuid & 0x3 | 0x8);
return v.toString(16);
});
}
for(var i = 0; i < 5; i++) {
console.log("A sample GUID / UUID:", generate_uuidv4());
}
A sample GUID / UUID: 3b869420-6618-43dd-a351-6373ed8ffb48 A sample GUID / UUID: 5d3459b9-d5f1-49ca-a356-d315b314b7eb A sample GUID / UUID: 19d3ea72-24c7-46e8-177c-a052eb9bc677 A sample GUID / UUID: e3f70fb6-951a-4ee1-b3a2-3173bf7e3a91 A sample GUID / UUID: eac986d7-2d1d-40a4-29eb-92eb131bd0cc
Enhanced Method with Timestamp
To reduce collision probability, we can incorporate the current timestamp:
function generate_uuidv4() {
var dt = new Date().getTime();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {
var rnd = Math.random() * 16; // random number in range 0 to 16
rnd = (dt + rnd) % 16 | 0;
dt = Math.floor(dt / 16);
return (c === 'x' ? rnd : (rnd & 0x3 | 0x8)).toString(16);
});
}
for(var i = 0; i < 5; i++) {
console.log("A sample GUID / UUID:", generate_uuidv4());
}
A sample GUID / UUID: 7a83193c-9624-48e2-a223-e910878c89c5 A sample GUID / UUID: c8e49a59-16a4-4d22-97fc-cdafffba2707 A sample GUID / UUID: 7feea4a6-59d5-4f0e-b7b7-b5cd2069fa0f A sample GUID / UUID: f244e83c-5af5-483b-8fdd-39d56c6b16ee A sample GUID / UUID: bc2e2167-91d5-4f53-afc3-af9efdc14e56
Using Pseudo-Random Character Generation
This method manually constructs a UUID by generating random hexadecimal characters and placing them in the correct positions:
function generate_uuidv4() {
var id_str = [];
var hxDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
id_str[i] = hxDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
id_str[14] = "4"; // version field
id_str[19] = hxDigits.substr((id_str[19] & 0x3) | 0x8, 1); // variant field
id_str[8] = id_str[13] = id_str[18] = id_str[23] = "-"; // hyphens
return id_str.join("");
}
for(var i = 0; i < 5; i++) {
console.log("A sample GUID / UUID:", generate_uuidv4());
}
A sample GUID / UUID: b5106962-5bc5-46dc-b74e-7a3fd0584fc0 A sample GUID / UUID: 5fdfa0e1-4251-4ae6-bafc-b162e032ab48 A sample GUID / UUID: de644da7-2dc4-499f-82fa-618f5b6f133e A sample GUID / UUID: cc5ce77f-18dd-46ba-8762-1cc7cf9c7a7f A sample GUID / UUID: 1708db50-0d8d-4fcc-8e7e-5cfc23387c6f
Simple Random String Method
A faster but less standard-compliant approach generates random alphanumeric strings:
function generate_simple_uuid() {
return Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
}
for(var i = 0; i < 5; i++) {
console.log("A sample GUID / UUID:", generate_simple_uuid());
}
A sample GUID / UUID: 8kyxdvlm8by3licwpf081c A sample GUID / UUID: coemu70fbbw9e8gsvvw8aq A sample GUID / UUID: 41kamutwz52izjiw1h769 A sample GUID / UUID: r3e4app5nfsphj3wa7sa8 A sample GUID / UUID: 463olfnw767sr9hy0hlcn
Comparison of Methods
| Method | Standard Compliant | Collision Resistance | Performance |
|---|---|---|---|
| Random Number Generation | Yes (UUID v4) | Good | Medium |
| Enhanced with Timestamp | Yes (UUID v4) | Better | Medium |
| Pseudo-Random Characters | Yes (UUID v4) | Good | Slower |
| Simple Random String | No | Lower | Fast |
Conclusion
For most applications, the random number generation method with timestamp enhancement provides the best balance of standard compliance and collision resistance. Use the simple string method only when performance is critical and uniqueness requirements are less strict.
