npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-unique

v1.2.0

Published

完全随机生成唯一字符串 · 超轻量 | Completely random generation of unique strings · Ultra-lightweight

Readme

简介

完全随机生成唯一字符串 · 超轻量

压缩大小: 180~ Byte

gzip 大小: 150~ bit

安装

Using npm:

npm install simple-unique --save

Using jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/simple-unique/dist/unique.min.js"></script>

Using unpkg CDN:

<script src="https://unpkg.com/simple-unique/dist/unique.min.js"></script>

使用案例

CLI

你可以全局安装后在终端使用

npm install simple-unique -g

你也可以使用npx免安装使用Simple-Unique

npx simple-unique
npx: installed 1 in 1.82s
3f7c94zs79

自定义唯一字符串长度

请注意,sunique是全局安装后,产生的别名,既可以使用sunique也可以使用simple-unique(推荐sunique)

sunique --size 20
y8n69mohr3pmf7vg19to

Node.js

const unique = require('simple-unique')
const arr1 = [unique(), unique()]
const arr2 = [unique(20), unique(20)]

console.log(arr1[0] === arr1[1], arr1) // false [ 'krntrfokkp', '35tftcm3tr' ]
console.log(arr2[0] === arr2[1], arr2) // false [ 'lva8timtvipgbenfjkhj', '9s32qj2chqulqxox7npq' ]

ES Modules

import unique from 'simple-unique'
const arr1 = [unique(), unique()]
const arr2 = [unique(20), unique(20)]

console.log(arr1[0] === arr1[1], arr1) // false [ 'sscya8ri7m', 'nbfrebutok' ]
console.log(arr2[0] === arr2[1], arr2) // false [ 'bcl5371wv3txd3d3a8zt', 'uvt8uiogn2jjbot9b07p' ]

浏览器

<script src="https://cdn.jsdelivr.net/npm/simple-unique/dist/unique.min.js"></script>
<script>
  const arr1 = [unique(), unique()]
  const arr2 = [unique(20), unique(20)]

  console.log(arr1[0] === arr1[1], arr1) // false [ 'gsf2e7u9fw', 'y2vm3x0tpv' ]
  console.log(arr2[0] === arr2[1], arr2) // false [ 'l399uakldce29mfev391', '36t375r2uim63wkplz1s' ]
</script>

Simple-Unique VS NanoID

Simple-UniqueNanoID 对比,分别生成9999999(一千万)个唯一 id,长度为 10 位数

如下结果是在浏览器控制台中进行测试的

Simple-Unique 的效率大约是 NanoID 的 7 倍

  • Simple-Unique: 3775.31591796875 ms (3~5 秒左右)
  • NanoID: 26226.182861328125 ms (26~28 秒左右)
// Simple-Unique
var SimpleUnique=function(){"use strict";return function(n){n=n||10;for(var r=function(){return Math.random().toString(36).slice(2)},t=r();t.length<n;)t+=r();return t.slice(0,n)}}();

// NanoID
let NanoID=(t=21)=>crypto.getRandomValues(new Uint8Array(t)).reduce(((t,e)=>t+=(e&=63)<36?e.toString(36):e<62?(e-26).toString(36).toUpperCase():e<63?"_":"-"),"");

const TenMillion = 9999999

var SimpleUniqueArray = []

var NanoIDArray = []

// 测试效率
// 差不多在3~5秒左右
// Simple-Unique: 3775.31591796875 ms
console.time('Simple-Unique')
for (let index = 0; index < TenMillion; index++) {
  SimpleUniqueArray.push(SimpleUnique(10))
}
console.timeEnd('Simple-Unique')

// 测试效率
// 差不多在26~28秒左右
// NanoID: 26226.182861328125 ms
console.time('NanoID')
for (let index = 0; index < TenMillion; index++) {
  NanoIDArray.push(NanoID(10))
}
console.timeEnd('NanoID')

// 测试在生成 9999999(一千万) 后是否存在重复id
// 使用 Set 对数组去重,最后查看剩余是否是 9999999(一千万)
const SimpleUniqueSize = new Set(SimpleUniqueArray).size
const NanoIDSize = new Set(NanoIDArray).size

// 如果为true,则表示在 9999999(一千万) 中无重复id
console.log(SimpleUniqueSize === TenMillion, SimpleUniqueSize) // true 9999999
console.log(NanoIDSize === TenMillion, NanoIDSize) // true 9999999