当前位置:网站首页>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 !边栏推荐
- China nylon 11 industry research and future forecast report (2022 Edition)
- China sorbitol Market Forecast and investment strategy report (2022 Edition)
- Oom caused by improper use of multithreading
- LeetCode中等题之TinyURL 的加密与解密
- DNS
- 走进微信小程序
- C language input / output stream and file operation
- China carbon disulfide industry research and investment strategy report (2022 Edition)
- Machine learning 11 clustering, outlier discrimination
- [pyg] document summary and project experience (continuously updated
猜你喜欢
随机推荐
Transition technology from IPv4 to IPv6
反射型XSS漏洞
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
redis -- 数据类型及操作
Redis6.0 新功能
SystemVerilog-结构体(二)
美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
(十六)ADC转换实验
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
剑指 Offer 20. 表示数值的字符串
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
Roewe rx5's "a little more" product strategy
走进微信小程序
Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
如何使用 etcd 实现分布式 /etc 目录
China sorbitol Market Forecast and investment strategy report (2022 Edition)
The amazing open source animation library is not only awesome, but also small
Iommu/smmuv3 code analysis (10) page table operation








![[C language foundation] 12 strings](/img/42/9c024eb08eb935fe66c3aaac7589d8.jpg)