当前位置:网站首页>How is nanoid faster and more secure than UUID implemented? (glory Collection Edition)
How is nanoid faster and more secure than UUID implemented? (glory Collection Edition)
2022-07-28 21:47:00 【Dragon back ride Shi】

Preface
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 .
1、 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.2.NanoID Only 108 Bytes
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 .
1.3. 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 , Called unified algorithm , Instead of using “ Random % alphabet ” random % alphabet.
We have created a high-quality technical exchange group , With good people , I will be excellent myself , Click Add group , Enjoy growing up together .
1.4. 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.
1.5. 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’
1.6. 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.
1.7. 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 .
2、 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 .
summary
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 .
边栏推荐
- 1162. Map analysis - non recursive method
- 百度搜索为什么只抓取,却不展现页面?
- Coolpad voluntarily terminated the patent infringement lawsuit against Xiaomi
- 中国农业工程学会农业水土工程专业委员会-第十二届-笔记
- Construction of Chinese traditional embroidery classification model based on xception TD
- 凡尔赛天花板:“毕业两年月薪才35K,真是没出息啊~~”
- 怎样巧用断言+异常处理类,使代码更简洁!(荣耀典藏版)
- ABB electromagnetic flowmeter maintenance signal transmitter maintenance 41f/e4 technical parameters
- 提前布局6G赛道!紫光展锐发布《6G无界 有AI》白皮书
- Hold high the two flags of 5g and AI: Ziguang zhanrui Market Summit is popular in Shencheng
猜你喜欢

Coding with these 16 naming rules can save you more than half of your comments!
![[Bluetooth Bluetooth development] VIII. Transmission layer of ble protocol](/img/43/7c3891befcb7cbb7dc67dfdeb763e2.png)
[Bluetooth Bluetooth development] VIII. Transmission layer of ble protocol

Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence

实现瀑布流效果

基于多模态融合的非遗图片分类研究

蚂蚁集团境外站点 Seata 实践与探索

聊一聊数据库的行存与列存

分而治之,大型文件分片上传

Analysis of critical path

Storage and steps of phospholipid coupled antibody / protein Kit
随机推荐
如何优雅的设计工作流引擎(荣耀典藏版)
基于BRNN的政务APP评论端到端方面级情感分析方法
中文招聘文档中专业技能词抽取的跨域迁移学习
How to skillfully use assertion + exception handling classes to make the code more concise! (glory Collection Edition)
Leetcode 142. circular linked list II [knowledge points: speed pointer, hash table]
Analysis of critical path
二 RedisTemplate的序列和反序列化机制讲解
小霸王被申请破产!公司成“老赖” ,法人被限制高消费
Bully is filed for bankruptcy! The company has become a "Lao Lai", and the legal person is restricted from high consumption
openEuler Embedded SIG | 分布式软总线
Storage and steps of phospholipid coupled antibody / protein Kit
Cy3/Cy5/Cy5.5/Cy7荧光标记抗体/蛋白试剂盒(10~100mg标记量)
日志瘦身神操作:从5G优化到1G到底是怎么做到的!(荣耀典藏版)
软考 --- 数据库(3)数据操作
作价11.5亿元,1206件设备注入合资公司!SK海力士抢食大陆晶圆代工市场!
Library borrowing system "suggested collection"
Bus, protocol, specification, interface, data acquisition and control system in industrial communication field
Explain C language 12 in detail (C language series)
[brother hero July training] day 28: dynamic planning
[极客大挑战 2019]Secret File&文件包含常用伪协议以及姿势