Several Ways of Generating Unique ID from JavaScript
- 2021-10-27 06:16:26
- OfStack
Feasible scheme
1. Math. random that generates a random number of [0, 1]
// What I generated this time is: 0.5834165740043102
Math.random();
2. Get the current timestamp Date. now
// Now the timestamp is 1482645606622
Date.now();
3. The string Number. toString that converts decimal to other decimal
// Will 1482645606622 Convert to 2 Binary system: 10101100100110100100100001001000011011110
(1482645606622).toString(2);
// Convert to 16 Binary system: 159349090de MongDB In ObjectID Is 24 Bit 16 Binary number
(1482645606622).toString(16);
// Maximum binary support conversion 36 In binary, the characters used are 0-9a-z : ix48wvry
(1482645606622).toString(36);
Improvement 1: Random Numbers + toString ()
1. Random number version
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(){
return Math.random().toString()
}
// Generate 1 A similarity 0.1283460319177394 Adj. ID
GenNonDuplicateID()
2. Random number version hexadecimal version
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(){
return Math.random().toString(16)
}
// Function generates a similar 0.c1615913fa915 Adj. ID
GenNonDuplicateID()
3. Random number version 36-ary version
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(){
return Math.random().toString(36)
}
// Function generates a similar 0.hefy7uw6ddzwidkwcmxkzkt9 Adj. ID
GenNonDuplicateID()
4. Random number version 36-ary version
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(){
return Math.random().toString(36).substr(2)
}
// Function generates a similar 8dlv9vabygks2cbg1spds4i Adj. ID
GenNonDuplicateID()
Summarize
Advantage: Using toString's binary conversion can achieve shorter string representation of more ranges
Disadvantages: Using one random number as ID, with the accumulation of usage times, the same two ID will inevitably appear
Modified version 2
1. Introducing timestamp + 36-ary version
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(){
let idStr = Date.now().toString(36)
idStr += Math.random().toString(36).substr(2)
return idStr
}
// Function generates a similar ix49sfsnt7514k5wpflyb5l2vtok9y66r Adj. ID
GenNonDuplicateID()
2. Introducing timestamp + 36-ary version + random number length control
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(randomLength){
let idStr = Date.now().toString(36)
idStr += Math.random().toString(36).substr(2,randomLength)
return idStr
}
// GenNonDuplicateID(3) Will generate a similar ix49wl2978w Adj. ID
GenNonDuplicateID(3)
However, the first few bits of ID generated in this way are always the same.
3. Introducing timestamp + random number preamble 36-ary + random number length control
/**
* Generate 1 Use non-repetitive ID
*/
function GenNonDuplicateID(randomLength){
return Number(Math.random().toString().substr(2,randomLength) + Date.now()).toString(36)
}
//GenNonDuplicateID() Will generate rfmipbs8ag0kgkcogc Similar ID
GenNonDuplicateID()
Summarize
Using only timestamps, there is one that can be accessed by multiple people in the same time. Adding random numbers can achieve only 1. Coupled with custom length, UUID is more flexible.
Summarize
Universal solution:
// Now the timestamp is 1482645606622
Date.now();
0
Above is the JavaScript generation only 1ID several ways of the details, more about JavaScript generation only 1ID information please pay attention to other related articles on this site!