当前位置:网站首页>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 !
边栏推荐
- (12) About time-consuming printing
- DNS
- How wild are hackers' ways of making money? CTF reverse entry Guide
- Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
- 拼接字符串,得到字典序最小的结果
- 荣威 RX5 的「多一点」产品策略
- String类
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- Integer array merge [JS]
- 可迭代对象与迭代器、生成器的区别与联系
猜你喜欢
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
Redis6.0 new features
(17) DAC conversion experiment
(28) Shape matching based on contour features
Gold, silver and four want to change jobs, so we should seize the time to make up
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
为什么你要考虑使用Prisma
官宣!香港科技大学(广州)获批!
SQL注入漏洞(Mysql与MSSQL特性)
换掉UUID,NanoID更快更安全!
随机推荐
如何写出好代码 — 防御式编程指南
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
为什么你要考虑使用Prisma
中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
(1) CNN network structure
LeetCode中等题之TinyURL 的加密与解密
In depth Research Report on China's disposable sanitary products production equipment industry (2022 Edition)
Hidden Markov model (HMM): model parameter estimation
Vulnhub range hacksudo Thor
Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
PHP实现敏感词过滤系统「建议收藏」
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
中国酶制剂市场预测与投资战略研究报告(2022版)
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
Jojogan practice
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
MySQL learning summary
Computed property “xxx“ was assigned to but it has no setter.