Several ways to generate unique IDs in JavaScript

Several ways to generate unique IDs in JavaScript

Possible solutions

1. Math.random generates random numbers in [0,1)

//This time I ran the generated value: 0.5834165740043102 
Math.random();

2. Get the current timestamp Date.now

//Now the timestamp is 1482645606622
Date.now();

3. Convert decimal to other base strings Number.toString

//Convert 1482645606622 into binary: 10101100100110100100100001001000011011110 
(1482645606622).toString(2);

//Convert to hexadecimal: 159349090de ObjectID in MongoDB is a 24-digit hexadecimal number (1482645606622).toString(16);

//The maximum base supported is hexadecimal, using characters 0-9a-z: ix48wvry 
(1482645606622).toString(36);

Improved version 1: Random number + toString()

1. Random number version

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(){
 return Math.random().toString()
}

//Generate an ID similar to 0.1283460319177394
GenNonDuplicateID()

2. Random number version hexadecimal version

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(){
 return Math.random().toString(16)
}

//The function will generate an ID like 0.c1615913fa915
GenNonDuplicateID()

3. Random number version hexadecimal version

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(){
 return Math.random().toString(36)
}

//The function will generate an ID like 0.hefy7uw6ddzwidkwcmxkzkt9
GenNonDuplicateID()

4. Random number version hexadecimal version

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(){
 return Math.random().toString(36).substr(2)
}

//The function will generate an ID similar to 8dlv9vabygks2cbg1spds4i
GenNonDuplicateID()

Summarize

Advantages: Using toString's base conversion can achieve shorter strings to represent a wider range of

Disadvantage: Using a random number as an ID will inevitably result in two identical IDs as the number of uses accumulates.

Improved version 2

1. Introduce timestamp + hexadecimal version

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(){
 let idStr = Date.now().toString(36)
 idStr += Math.random().toString(36).substr(2)
 return idStr
}

//The function will generate an ID similar to ix49sfsnt7514k5wpflyb5l2vtok9y66r
GenNonDuplicateID()

2. Introduce timestamp + hexadecimal version + random number length control

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(randomLength){
 let idStr = Date.now().toString(36)
 idStr += Math.random().toString(36).substr(2,randomLength)
 return idStr
}

// GenNonDuplicateID(3) will generate an ID similar to ix49wl2978w
GenNonDuplicateID(3)

However, the first few digits of the ID generated in this way are always the same.

3. Introduce timestamp + prepend hexadecimal to random number + control random number length

/**
 * Generate a unique ID
 */
function GenNonDuplicateID(randomLength){
 return Number(Math.random().toString().substr(2,randomLength) + Date.now()).toString(36)
}
//GenNonDuplicateID() will generate an ID similar to rfmipbs8ag0kgkcogc
GenNonDuplicateID()

Summarize

Just use the timestamp so that multiple people can access the same data at the same time. Adding random numbers can achieve uniqueness. Plus custom length, UUID is more flexible.

Summarize

Universal solution:

/**
 * Generate a unique ID
 * @param { Number } randomLength 
 */
function getUuiD(randomLength){
 return Number(Math.random().toString().substr(2,randomLength) + Date.now()).toString(36)
}

The above are the details of several ways to generate unique IDs with JavaScript. For more information about how to generate unique IDs with JavaScript, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Javascript method to generate globally unique identifier (GUID, UUID)
  • Each component in extjs must have a unique ID, otherwise an error will occur

<<:  Linux forced release of occupied ports and Linux firewall port opening method detailed explanation

>>:  Summary of the operation records of changing MyISAM storage engine to Innodb in MySQL

Recommend

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...

Clever use of webkit-box-reflect to achieve various dynamic effects (summary)

In an article a long time ago, I talked about the...

Example code for circular hover effect using CSS Transitions

This article introduces Online preview and downlo...

Detailed tutorial on installing Mysql5.7.19 on Centos7 under Linux

1. Download MySQL URL: https://dev.mysql.com/down...

Solve the problem that vue project cannot carry cookies when started locally

Solve the problem that the vue project can be pac...

Vue.js cloud storage realizes image upload function

Preface Tip: The following is the main content of...

Solve the problem that the IP address obtained using nginx is 127.0.0.1

Get ip tool import lombok.extern.slf4j.Slf4j; imp...

MySQL 8.0.11 MacOS 10.13 installation and configuration method graphic tutorial

The process of installing MySQL database and conf...

Docker's health detection mechanism

For containers, the simplest health check is the ...

Issues with Rancher deployment and importing K8S clusters

Rancher deployment can have three architectures: ...

Quick understanding of Vue routing navigation guard

Table of contents 1. Global Guard 1. Global front...

Sample code for batch deployment of Nginx with Ansible

1.1 Copy the nginx installation package and insta...