当前位置:网站首页>Replace UUID, nanoid is faster and safer!
Replace UUID, nanoid is faster and safer!
2022-07-01 17:18:00 【Java notes shrimp】
Click on the official account , utilize Fragment time to learn
UUID It is one of the most commonly used general identifiers in software development . However , In the past few years , Other competing products challenge its existence .
among ,NanoID yes UUID One of our main competitors .
therefore , In this paper , We will discuss NanoID The function of 、 Its highlights and its limitations , So that we can better understand when to use it .
understand NanoID And its usage
about JavaScript, Generate UUID or NanoID It's very simple . They all have corresponding NPM Package to help us realize the generation .
All we need to do is run npm i nanoid Command to install NanoID NPM library And use it in our project :
import { nanoid } from 'nanoid';
model.id = nanoid();Do you know NanoID Weekly NPM Download more than 1175.4 ten thousand , And it works better than UUID fast 60%?
Besides ,NanoID Than UUID Nearly younger 7 year , And it's GitHub The number of stars is more than UUID many .
The following figure shows the relationship between the two npm Trend comparison , We can see NanoID The rising trend of UUID There is a strong contrast between the flat progress of .

I hope these figures have convinced you to try NanoID.
however , The main difference between the two is simple . It comes down to the alphabet used by the key .
because NanoID Use than UUID The bigger alphabet , So the shorter ID Can be used with longer UUID For the same purpose .
1. NanoID Only 108 The size of a byte
And UUID Different ,NanoID It's smaller 4.5 times , And there's no dependency . Besides , The size limit has been used to reduce the size from another 35% Reduce .
Size reduction directly affects the size of the data . for example , Use NanoID Small and compact objects , It can be used for data transmission and storage . As applications grow , These numbers become obvious .
2. More secure
In most random generators , They use unsafe Math.random(). however ,NanoID Use crypto module and Web Crypto API, signify NanoID More secure .
Besides ,NanoID stay ID The implementation of the generator uses its own algorithm , be called Unified algorithm , Instead of using “ Random % alphabet ” random % alphabet.
3. It's fast and compact
NanoID Than UUID fast 60%. And UUID In the alphabet 36 The characters are different ,NanoID Only 21 Characters .
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-Besides ,NanoID Support 14 Different programming languages , They are :
C#、C++、Clojure and ClojureScript、Crystal、Dart & Flutter、Deno、Go、Elixir、Haskell、Janet、Java、Nim、Perl、PHP、 With a dictionary Python、Ruby、Rust、Swift
4. Compatibility
It also supports PouchDB、CouchDB WebWorkers、Rollup as well as React and Reach-Native Such as the library .
We can use npx nanoid Get unique in the terminal ID. stay JavaScript Use in NanoID The only requirement is to install NodeJS.

Besides , We can still do that Redux toolkit Find NanoID, And use it for other use cases , As shown below ;
import { nanoid } from ‘@reduxjs/toolkit’
console.log(nanoid()) //‘dgPXxUz_6fWIQBD8XmiSy’5. Custom letters
NanoID Another existing feature of is that it allows developers to use a custom alphabet . We can change the text or id Size , As shown below :
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('ABCDEF1234567890', 12);
model.id = nanoid(); In the example above , I define the custom alphabet as ABCDEF1234567890, And will Id The size of is defined as 12.
6. There is no third party to rely on
because NanoID Don't rely on any third party , as time goes on , It can become more stable and autonomous .
In the long run , This helps to optimize the package size , And make it less prone to dependency problems .
Limitations and future priorities
according to StackOverflow Many of the experts in this field , Use NanoID There are no obvious shortcomings or limitations .
Non human readable is something that many developers are doing NanoID The main drawbacks seen in , Because it makes debugging more difficult . however , And UUID comparison ,NanoID Shorter and readable .
in addition , If you use NanoID As the primary key of the table , There will also be problems if you use the same column as the clustered index . This is because NanoID Not continuous .
In the future ……
NanoID Is gradually becoming JavaScript The only most popular id generator , Most developers prefer to choose it rather than UUID.

The above benchmark shows NanoID With other major id Performance compared to the generator .
Using the default alphabet, more than... Can be generated per second 220 Ten thousand unique ID, More than... Can be generated per second using a custom alphabet 180 Ten thousand unique ID.
According to my use UUID and NanoID Experience , Considering its small size 、URL Friendliness 、 Safety and speed , I recommend using... In any future project NanoID instead of UUID.
therefore , I invite you to try... In the next project NanoID, And share your thoughts with others in the comments section .
Original address :Why is NanoID Replacing UUID
Original author :Charuka Herath
The translation comes from : Nuggets translation plan Permanent link to this article :https://github.com/xitu/gold-miner/blob/master/article/2021/why-is-nanoid-replacing-uuid.md
translator : Frost plume Hoarfroster
recommend :
The most comprehensive java Interview question bank
PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !边栏推荐
- SQL question brushing 586 Customers with the most orders
- Develop those things: easycvr cluster device management page function display optimization
- Redis Distributed Lock
- China nylon 11 industry research and future forecast report (2022 Edition)
- Transition technology from IPv4 to IPv6
- The difference between the lazy mode of singleton mode and the evil mode
- Vulnhub range hacker_ Kid-v1.0.1
- Leetcode records - sort -215, 347, 451, 75
- Soft test software designer full truth simulation question (including answer analysis)
- Determine whether the linked list is a palindrome linked list
猜你喜欢
Roewe rx5's "a little more" product strategy

Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!

C語言輸入/輸出流和文件操作

ACL 2022 | 分解的元学习小样本命名实体识别

(28) Shape matching based on contour features

【C语言基础】12 字符串

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放

Detailed explanation of string's trim() and substring()

【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
随机推荐
Judge whether the binary tree is a binary search tree
(1) CNN network structure
(28) Shape matching based on contour features
Cookies and session keeping technology
SystemVerilog structure (II)
mysql -- explain性能优化
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
(27) Open operation, close operation, morphological gradient, top hat, black hat
Introduction to software engineering - Chapter 6 - detailed design
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
Pyqt5, draw a histogram on the control
越来越多地使用 SLO 来实现可观测性|DevOps
Redis6.0 新功能
SystemVerilog-结构体(二)
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
String class
Roewe rx5's "a little more" product strategy
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
多线程并发之CountDownLatch阻塞等待