当前位置:网站首页>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 !
边栏推荐
- 开发那些事儿:EasyCVR集群设备管理页面功能展示优化
- Develop those things: easycvr cluster device management page function display optimization
- Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
- Concatenate strings to get the result with the smallest dictionary order
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- 深度优先遍历和广度优先遍历[通俗易懂]
- Mysql database - Advanced SQL statement (2)
- Leetcode records - sort -215, 347, 451, 75
- Why should you consider using prism
- 官宣!香港科技大学(广州)获批!
猜你喜欢
【Try to Hack】vulnhub DC4
How to use etcd to realize distributed /etc directory
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
Shenyu gateway development: enable and run locally
Gold, silver and four want to change jobs, so we should seize the time to make up
Vulnhub range hacksudo Thor
vulnhub靶场-Hacker_Kid-v1.0.1
Roewe rx5's "a little more" product strategy
Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
(16) ADC conversion experiment
随机推荐
unity3d扩展工具栏
(28) Shape matching based on contour features
China metallocene polyethylene (MPE) Industry Research Report (2022 Edition)
China carbon disulfide industry research and investment strategy report (2022 Edition)
Leetcode records - sort -215, 347, 451, 75
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
Integer array merge [JS]
SQL question brushing 584 Looking for user references
Openlayers customize bubble boxes and navigate to bubble boxes
[C language supplement] judge which day tomorrow is (tomorrow's date)
ACL 2022 | 分解的元学习小样本命名实体识别
The amazing open source animation library is not only awesome, but also small
存在安全隐患 起亚召回部分K3新能源
【Try to Hack】vulnhub DC4
[flask introduction series] cookies and session
vulnhub靶场-hacksudo - Thor
Soft test software designer full truth simulation question (including answer analysis)
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
官宣!香港科技大学(广州)获批!
中国茂金属聚乙烯(mPE)行业研究报告(2022版)