当前位置:网站首页>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 !边栏推荐
- 剑指 Offer 20. 表示数值的字符串
- Pytest learning notes (13) -allure of allure Description () and @allure title()
- Soft test network engineer full truth simulation question (including answer and analysis)
- 单例模式的懒汉模式跟恶汉模式的区别
- 开发那些事儿:EasyCVR集群设备管理页面功能展示优化
- JDBC:深入理解PreparedStatement和Statement[通俗易懂]
- 中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
- SQL question brushing 586 Customers with the most orders
- (十七)DAC转换实验
- [pyg] document summary and project experience (continuously updated
猜你喜欢

【PyG】文档总结以及项目经验(持续更新

Leetcode records - sort -215, 347, 451, 75

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

How to use etcd to realize distributed /etc directory

机器学习11-聚类,孤立点判别

Basic usage of Frida

How wild are hackers' ways of making money? CTF reverse entry Guide

【C语言基础】12 字符串

String类

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
随机推荐
Please, stop painting star! This has nothing to do with patriotism!
【C语言补充】判断明天是哪一天(明天的日期)
Encryption and decryption of tinyurl in leetcode
如何写出好代码 — 防御式编程指南
Redis 分布式鎖
中国一次性卫生用品生产设备行业深度调研报告(2022版)
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
Iommu/smmuv3 code analysis (10) page table operation
redis -- 数据类型及操作
(17) DAC conversion experiment
Judge whether the binary tree is a binary search tree
libcurl下载文件的代码示例
走进微信小程序
越来越多地使用 SLO 来实现可观测性|DevOps
Hidden Markov model (HMM): model parameter estimation
MySQL learning summary
China PBAT resin Market Forecast and Strategic Research Report (2022 Edition)
C language input / output stream and file operation
[flask introduction series] cookies and session
[pyg] document summary and project experience (continuously updated