JS method to generate id (multiple solutions)

Introduction

Generating unique IDs is a crucial task in front-end development. There are several common ways to generate IDs in JavaScript, some of which are covered in this article.

use random numbers

We can use random numbers to generate unique IDs. We can use the Math.random() method to generate a random number, convert it to a string, and add some prefix or suffix to generate a unique ID. The code example is as follows:

const uniqueId = 'id-' + Math.random().toString(36).substr(2, 9);
console.log(uniqueId);

This approach is straightforward, but the random numbers may generate the same value, so uniqueness is not guaranteed.

Based on timestamp and random number

We can combine the current timestamp and a random number to generate a unique ID. The code example is as follows:

const uniqueId = 'id-' + new Date().getTime().toString(36) + '-' + Math.random().toString(36).substr(2, 9);
console.log(uniqueId);

This method can ensure uniqueness, but the ID is relatively long and not suitable for some scenarios.

Based on performance counters

We can use the window.performance.now() method to generate performance counters and convert them to strings as IDs. The code example is as follows:

const uniqueId = 'id-' + window.performance.now().toString().replace('.', '');
console.log(uniqueId);

This approach ensures uniqueness, but requires browser support for performance APIs.

Use third-party libraries

We can generate unique IDs using third party libraries like shortid and nanoid. These libraries provide more options to generate IDs, such as custom length, charset and generation rules. The code example is as follows:

const shortid = require('shortid');
const uniqueId = shortid.generate();
console.log(uniqueId);

Using a third-party library can easily and quickly generate a unique ID, but it depends on the third-party library.

UUID

UUID is an abbreviation for Universally Unique Identifier, an identifier generated by an algorithm that ensures global uniqueness. In JavaScript, we can generate UUIDs using third-party libraries like uuid. The code example is as follows:

const uuid = require('uuid');
const uniqueId = uuid.v4();
console.log(uniqueId);

Using UUID can ensure uniqueness, but the ID is relatively long and not suitable for some scenarios.

Use object or array length

We can use arrays, lengths of objects, etc. as IDs. The code example is as follows:

const array = [1, 2, 3];
const uniqueId = 'id-' + array.length;
console.log(uniqueId);

This method is simple and easy to understand, but it needs to ensure the uniqueness of the object or array.

increasing sequence

We can use an incrementing sequence to generate a unique ID. The code example is as follows:

let count = 0;
const uniqueId = 'id-' + (count++);
console.log(uniqueId);

This method is simple and easy to understand, and can ensure uniqueness, but it needs to pay attention to concurrency issues in a multi-threaded environment.

in conclusion

This article describes several common ways to generate unique IDs in JavaScript: using random numbers, based on timestamps and nonces, based on performance counters, using third-party libraries, UUIDs, using object or array lengths and incrementing sequences. According to different needs and scenarios, we can choose different methods to generate a unique ID. When using these methods, we need to consider uniqueness and performance issues. Hope this article can help you!

Guess you like

Origin blog.csdn.net/achen0511/article/details/130723247