| Crates.io | ridgen |
| lib.rs | ridgen |
| version | 0.1.0 |
| created_at | 2026-01-21 14:39:38.29725+00 |
| updated_at | 2026-01-21 14:39:38.29725+00 |
| description | A lightweight, high-performance Rust library for generating secure, URL-friendly unique string IDs, with WASM support. |
| homepage | |
| repository | https://github.com/Taniyama2024/ridgen |
| max_upload_size | |
| id | 2059435 |
| size | 78,451 |
日本語 | English
A lightweight, high-performance Rust library for generating secure, URL-friendly unique string IDs.
All tests performed with 1,000,000 iterations:
| Library | Language | Time (ms) | Throughput (million IDs/sec) | Notes |
|---|---|---|---|---|
| ridgen | Rust | ~340 | ~3.0 | This implementation ⚡ |
| uuid (v4) | JavaScript | 375 | ~2.7 | Standard UUID v4 |
| nanoid | JavaScript | 431 | ~2.3 | Widely used original |
| shortid | JavaScript | 3,220 | ~0.3 | Deprecated, slower |
| cuid2 | JavaScript | 34,274 | ~0.03 | Collision-resistant |
Note: The comparison is against JavaScript implementations running on Node.js. Rust implementations of UUID are not included in this benchmark.
Add this to your Cargo.toml:
[dependencies]
ridgen = "0.1.0"
npm install ridgen
use ridgen::generate;
fn main() {
// Generate a 16-character ID
match generate(16) {
Ok(id) => println!("Generated ID: {}", id),
Err(e) => eprintln!("Error: {:?}", e),
}
}
This package is built with WASM and supports modern environments.
import { generate } from "ridgen";
const id = generate(16);
console.log(`Generated ID: ${id}`);
[!IMPORTANT]
- ESM Only: This package only supports ECMAScript Modules (ESM). CommonJS (
require) is not supported.- WASM Initialization: In environments that support top-level WASM (like Bun or Webpack), it works immediately. For other environments, ensure your bundler is configured to handle
.wasmfiles.
Performance test with 1,000,000 iterations generating 16-character IDs:
- Total time: ~340ms (average)
- Throughput: ~2.9-3.0 million IDs/sec
Note: Uses thread_rng (Default) for random number generation.
Why no SmallRng?: Although SmallRng is faster, it is not cryptographically secure. ridgen prioritizes security by using thread_rng to ensure IDs are unpredictable, while maintaining high performance through batch generation.
Optimization: Uses batch random number generation (RngCore::fill_bytes) instead of calling gen_range() for each character, reducing CPU instruction count and improving performance by ~20-25%.
The library uses a URL-safe character set:
a-zA-Z0-9Total: 62 characters
The generate function returns a Result<String, RidgenError>:
Ok(String): Successfully generated IDErr(RidgenError::InvalidLength): If length is 0unsafe UsageThis library uses a small amount of unsafe code for performance reasons.
Specifically, String::from_utf8_unchecked is used when constructing the final ID string.
a-zA-Z0-9)Because these invariants are strictly controlled, skipping UTF-8 validation is safe and avoids unnecessary runtime checks.
If the character set is ever modified to include non-ASCII characters, this unsafe usage must be revisited.
This implementation uses a simple modulo operation to map random bytes to the character set:
index = random_byte % 62
Since 256 is not evenly divisible by 62, this introduces a small distribution bias:
This library prioritizes performance and simplicity over perfect uniform distribution. If strict cryptographic uniformity is required, consider using rejection sampling or other methods.
cargo run --release
cd ../js-nanoid-bench
npm install
node benchmark.js
MIT
Contributions are welcome! Please feel free to submit a Pull Request.