一个微小(134B) 快速的实用程序,用于生成固定长度的随机ID

网友投稿 768 2022-10-14

一个微小(134B) 快速的实用程序,用于生成固定长度的随机ID

一个微小(134B) 快速的实用程序,用于生成固定长度的随机ID

A tiny (134B) and fast utility to generate random IDs of fixed length

Fast object IDs. Available for Node.js and the browser.Generate randomized output strings of fixed length using lowercase alphanumeric characters (a-z0-9).

Notice: Please note that this is not a cryptographically secure (CSPRNG) generator.

Additionally, this module is delivered as:

CommonJS: dist/index.jsES Module: dist/index.mjsUMD: dist/index.min.js

Install

$ npm install --save foid

Usage

import foid from 'foid';// length = 11 (default)foid(); //=> 'fsm2vsgo1pr'foid(); //=> 'gf34sezvoh6'// customize lengthfoid(16); //=> 'zbb6cc3ay26omrdz'foid(25); //=> 'lljjmo3f39rnjudsgqvzta1rb'foid(32); //=> 'yrfiw88qlq1fgpm40lguz6u43gksfj4a'

API

foid(length?)

Returns: String

Creates a new random identifer of fixed length.

length

Type: Number Default: 11

Then length of the output string.

Important: Your risk of collisions decreases with longer strings!

Benchmarks

Running on Node.js v10.13.0

Validation (length = 11): ✔ hashids/fixed (example: "QWjnegYbwZ1") ✔ nanoid (example: "bg7EJCm_Nqr") ✔ foid (example: "20075xrt1pu")Benchmark (length = 11): hashids/fixed x 358,492 ops/sec ±2.03% (98 runs sampled) nanoid x 455,771 ops/sec ±0.23% (98 runs sampled) foid x 4,917,033 ops/sec ±0.44% (93 runs sampled)Validation (length = 16): ✔ hashids/fixed (example: "LkQWjnegYbwZ1p0G") ✔ nanoid (example: "AAT4FkNDn-aHq85-") ✔ foid (example: "o35hr3fodrvl08wd")Benchmark (length = 16): hashids/fixed x 359,151 ops/sec ±0.20% (97 runs sampled) nanoid x 475,616 ops/sec ±0.22% (95 runs sampled) foid x 3,622,540 ops/sec ±0.16% (91 runs sampled)Validation (length = 25): ✔ cuid (example: "ck7bmh0fy0000ig7c516i9dx3") ✔ hashids/fixed (example: "r9JOyLkQWjnegYbwZ1p0GDXNm") ✔ nanoid (example: "xzNUa5jeWSUFAB9jzmaZpCMaV") ✔ foid (example: "3q87leuz1iiu4easjqsrgcj5i")Benchmark (length = 25): cuid x 158,939 ops/sec ±0.61% (93 runs sampled) hashids/fixed x 330,830 ops/sec ±2.90% (96 runs sampled) nanoid x 433,802 ops/sec ±0.34% (93 runs sampled) foid x 2,446,284 ops/sec ±0.39% (97 runs sampled)Validation (length = 36): ✔ uuid/v1 (example: "9dbdc9b0-5d27-11ea-a6bf-3be13a6bca5a") ✔ uuid/v4 (example: "d1846c4b-6794-4c1e-89ea-30ed21e24c40") ✔ hashids/fixed (example: "EVq3Pr9JOyLkQWjnegYbwZ1p0GDXNmRBlAxg") ✔ @lukeed/uuid (example: "eb9fac31-53af-4699-9205-9b1d23474b6b") ✔ nanoid (example: "wf9fq-M_-1SCrSBQEBOtFhLgGm2LLTeuiJKl") ✔ foid (example: "k98vi4c6lf77oc8ngerrkv3xtr0nvtkup1mg")Benchmark (length = 36): uuid/v1 x 1,509,781 ops/sec ±0.16% (98 runs sampled) uuid/v4 x 335,638 ops/sec ±1.02% (91 runs sampled) hashids/fixed x 322,714 ops/sec ±0.20% (97 runs sampled) @lukeed/uuid x 6,368,185 ops/sec ±0.23% (96 runs sampled) nanoid x 399,746 ops/sec ±0.26% (96 runs sampled) foid x 1,772,239 ops/sec ±0.18% (97 runs sampled)

Related

@lukeed/uuid - A tiny (230B), fast, and cryptographically secure UUID (V4) generator for Node and the browser

License

MIT © Luke Edwards

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:diffy:用于构建基于diff交互式命令行工具的一个小框架
下一篇:关于版本号的定义和规划
相关文章

 发表评论

暂时没有评论,来抢沙发吧~