Skip to content
Advertisement

How to generate short uid like “aX4j9Z” (in JS)

For my web application (in JavaScript) I want to generate short guids (for different objects – that are actually different types – strings and arrays of strings)

I want something like “aX4j9Z” for my uids (guids).

So these uids should be lightweight enough for web transfer and js string processing and quite unique for not a huge structure (not more than 10k elements). By saying “quite unique” I mean that after the generation of the uid I could check whether this uid does already exist in the structure and regenerate it if it does.

Advertisement

Answer

See @Mohamed’s answer for a pre-packaged solution (the shortid package). Prefer that instead of any other solutions on this page if you don’t have special requirements.


A 6-character alphanumeric sequence is pretty enough to randomly index a 10k collection (366 = 2.2 billion and 363 = 46656).

function generateUID() {
    // I generate the UID from two parts here 
    // to ensure the random number provide enough bits.
    var firstPart = (Math.random() * 46656) | 0;
    var secondPart = (Math.random() * 46656) | 0;
    firstPart = ("000" + firstPart.toString(36)).slice(-3);
    secondPart = ("000" + secondPart.toString(36)).slice(-3);
    return firstPart + secondPart;
}

UIDs generated randomly will have collision after generating ~ √N numbers (birthday paradox), thus 6 digits are needed for safe generation without checking (the old version only generates 4 digits which would have a collision after 1300 IDs if you don’t check).

If you do collision checking, the number of digits can be reduced 3 or 4, but note that the performance will reduce linearly when you generate more and more UIDs.

var _generatedUIDs = {};
function generateUIDWithCollisionChecking() {
    while (true) {
        var uid = ("0000" + ((Math.random() * Math.pow(36, 4)) | 0).toString(36)).slice(-4);
        if (!_generatedUIDs.hasOwnProperty(uid)) {
            _generatedUIDs[uid] = true;
            return uid;
        }
    }
}

Consider using a sequential generator (e.g. user134_item1, user134_item2, …) if you require uniqueness and not unpredictability. You could “Hash” the sequentially generated string to recover unpredictability.

UIDs generated using Math.random is not secure (and you shouldn’t trust the client anyway). Do not rely on its uniqueness or unpredictability in mission critical tasks.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement