How to generate unique ID with node.js

前端 未结 14 1880
感动是毒
感动是毒 2020-12-12 10:17
function generate(count) {
    var founded = false,
        _sym = \'abcdefghijklmnopqrstuvwxyz1234567890\',
        str = \'\';
    while(!founded) {
        for(va         


        
14条回答
  •  旧时难觅i
    2020-12-12 11:10

    used https://www.npmjs.com/package/uniqid in npm

    npm i uniqid
    

    It will always create unique id's based on the current time, process and machine name.

    • With the current time the ID's are always unique in a single process.
    • With the Process ID the ID's are unique even if called at the same time from multiple processes.
    • With the MAC Address the ID's are unique even if called at the same time from multiple machines and processes.

    Features:-

    • Very fast
    • Generates unique id's on multiple processes and machines even if called at the same time.
    • Shorter 8 and 12 byte versions with less uniqueness.

提交回复
热议问题