Thursday, October 11, 2018

Guide to Unique identifiers - UUID, GUID, UDID - Javascript | Typescript

In this blog post, We are going to learn how to generate Unique Identifiers - UUID, GUID, UDID  in javascript/typescript/nodejs.
UUID, GUID, UDID  examples with javascript typescript

Unique Identifier Generator

In every programming language, We have a unique identifier to identify the resources or value or references. There are various names that identify the unique references. Generally Following are the frequently used in javascript applications.
  • Universally Unique Identifier - UUID
  • Globally Unique Identifier - GUID
  • Unique Device Identifier - UDID

Universally Unique Identifier - UUID

UUID is a universal unique identifier. It contains 16 bytes value that can be used to identify unique value.The value represents hexadecimal values. This can be used as a primary key in the database tables to uniquely identify the rows
16 bytes values are uniquely defined by RFC 4122

Globally Unique Identifier - GUID

GUID is Microsoft version of UUID. You can user alternatively both and functionality and usage are same.

Unique Device Identifier - UDID

This is mostly used in mobiles to uniquely identify the devices. It contains  40 hexadecimal characters in size. It can be mostly used in IOS devices to identify the device

Difference between GUID and UUID

Both uniquely identify the value. UUID scope is universal, GUID scope is global. GUID is Microsoft implementation of UUID. Both generate a 16 bytes characters separated in 5 groups hyphen 

Here is sample Unique identifier
38bcc565-59f8-4506-818c-9d9dd7cb7b11

GUID UUID uses

  • Primary keys in Database tables to uniquely identify rows
  • values to identify windows registries to identify windows resources
  • To identify the data spread across servers in big data 
  • Used in REST API for doing CRUD operations using the identifier

javascript - How to create a UUID or GUID with an example

There are various ways to create a GUID or UUID. This example creates UUID, GUID in javascript
function uuid() {
  var uuidValue = "", k, randomValue;
  for (k = 0; k < 32;k++) {
    randomValue = Math.random() * 16 | 0;

    if (k == 8 || k == 12 || k == 16 || k == 20) {
      uuidValue += "-"
    }
    uuidValue += (k == 12 ? 4 : (k == 16 ? (randomValue & 3 | 8) : randomValue)).toString(16);
  }
  return uuidValue;
}
console.log(uuid())
output is
c50a2fbe-99b9-4d90-b332-a1a054c946b4

Generate Unique Identifier in Nodejs with an example

npm package uuid is available to generate Unique identifier First install using below command

npm install uuid
Here is an example usage

const uuid = require('uuid/v1');
uuidv1(); 
Output is
6a72d3cd-2d53-4e0e-8acb-0e1c98331fe1

Typescript - generate UUID or GUID with an example 

In typescript applications, You can use npm package UUID. first install uuid package using npm install uuid command. In typescript file, import uuid package and directly use uuid() function as below
import { v1 as uuid } from 'uuid';
const id: string = uuid();

Related article


EmoticonEmoticon