Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of UUID and NanoID generated by JS in a unique id way

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you the content of the sample analysis of UUID and NanoID in which JS generates the only id. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Why is NanoID replacing UUID

1 、. Safer in most random generators, they use the insecure Math.random (). However, the use of crypto module and Web Crypto API for NanoID means that NanoID is more secure. In addition, NanoID uses its own algorithm in the implementation of the ID generator, called the unified algorithm, rather than using the "random% alphabet" random% alphabet.

2. It is fast and compact. NanoID is 60% faster than UUID. Unlike the 36 characters in the UUID alphabet, NanoID has only 21 characters.

In addition, NanoID supports 14 different programming languages, which are:

C #, C++, Clojure and ClojureScript, Crystal, Dart & Flutter, Deno, Go, Elixir, Haskell, Janet, Java, Nim, Perl, PHP, Python, Ruby, Rust, Swift with dictionary

3. Compatibility

It also supports libraries such as PouchDB, CouchDB WebWorkers, Rollup, and React and Reach-Native. We can use npx nanoid to get a unique ID in the terminal.

2. How to generate js

Let's take a look at how they are generated with js.

First of all, how did we generate uuid before?

Method 1:

Function guid () {return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace (/ [xy] / g, function (c) {var r = Math.random () * 16 | 0, v = c = =' x'? R: (r & 0x3 | 0x8); return v.toString (16);} guid () / / "a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7"

Method 2:

Var _ S4 = function () {return (1 + Math.random ()) * 0x10000) | 0) .toString (16) .substring (1)} / / the only way to get an ad request is the current timestamp + 13-bit random export function GetsingleId () {var _ res = (_ S4 () + _ S4 () +'- + _ S4 () +'-'+ _ S4 () +'- + _ S4 () +'- + _ S4 () +'-'+ _ S4 ()) S4 () + _ S4 () + _ S4 () return'_'+ config.version +'_'+ _ res}

Method 3:

Function uuid () {var s = []; var hexDigits = "0123456789abcdef"; for (var I = 0; I

< 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid;}uuid() // "ffb7cefd-02cb-4853-8238-c0292cf988d5"三、NanoID的方式 import { nanoid } from 'nanoid'let idA = nanoid() //=>

"V1StGXR8_Z5jdHi6B-myT" / / can also specify the length of the generated string let idB = nanoid (5)

You can install it mainly in the form of npm package. The core code is as follows:

Let urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'let nanoid = (size = 21) = > {let id =''/ / A compact alternative for `for (var I = 0; I < step; iTunes +) `. Let I = size while (iMury -) {/ / `| 0`is more compact and faster than `Math.floor ()`. Id + = urlAlphabet [(Math.random () * 64) | 0]} return id} Thank you for reading! This is the end of this article on "JS generates an example analysis of UUID and NanoID in a unique id way". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report