How to generate unique ID with node.js

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


        
相关标签:
14条回答
  • 2020-12-12 11:02

    More easy and without addition modules

    Math.random().toString(26).slice(2)
    
    0 讨论(0)
  • 2020-12-12 11:03

    to install uuid

    npm install --save uuid
    

    uuid is updated and the old import

    const uuid= require('uuid/v4');
    

    is not working and we should now use this import

    const {v4:uuid} = require('uuid');
    

    and for using it use as a funciton like this

    const  createdPlace = {
        id: uuid(),
        title,
        description,
        location:coordinates,
        address,
        creator
      };
    
    0 讨论(0)
  • 2020-12-12 11:04

    The fastest possible way to create random 32-char string in Node is by using native crypto module:

    const crypto = require("crypto");
    
    const id = crypto.randomBytes(16).toString("hex");
    
    console.log(id); // => f9b327e70bbcf42494ccb28b2d98e00e
    
    0 讨论(0)
  • 2020-12-12 11:05

    I am using the following and it is working fine plus without any third-party dependencies.

    const {
      randomBytes
    } = require('crypto');
    
    const uid = Math.random().toString(36).slice(2) + randomBytes(8).toString('hex') + new Date().getTime();
    
    0 讨论(0)
  • 2020-12-12 11:06

    Extending from YaroslavGaponov's answer, the simplest implementation is just using Math.random().

    Math.random()
    

    The chances of fractions being the same in a real space [0, 1] is theoratically 0 and approximately close to 0 for a default length of 16 decimals in node.js. And this implementation should also reduce arithmetic overflows as no operations are performed. Also, it is more memory efficient compared to a string as Decimals occupy less memory than strings.

    I call this the "Chong-Fractional-Unique-ID".

    Wrote code to generate 1,000,000 Math.random() numbers and could not find any duplicates (at least for default decimal points of 16). See code below (please provide feedback if any):

    random_numbers = [] 
    for (i = 0; i < 1000000; i++) { 
       random_numbers.push(Math.random()) 
       //random_numbers.push(Math.random().toFixed(13)) //depends decimals default 16 
    } 
    
    if (i === 1000000) { 
       console.log("Before checking duplicate") 
       console.log(random_numbers.length) 
       console.log("After checking duplicate") 
       random_set = new Set(random_numbers) 
       console.log([...random_set].length) // length is still the same
    } 
    
    0 讨论(0)
  • 2020-12-12 11:07

    node-uuid is deprecated so please use uuid

    npm install uuid --save
    // Generate a v1 UUID (time-based) 
    const uuidV1 = require('uuid/v1');
    uuidV1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a' 
    
    // Generate a v4 UUID (random) 
    const uuidV4 = require('uuid/v4');
    uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1' 
    

    Npm link

    0 讨论(0)
提交回复
热议问题