sparkid
v1.2.0
Published
Fast, time-sortable, 22-char Base58 unique ID generator
Maintainers
Readme
sparkid
Fast, monotonic, time-sortable, 22-char Base58 unique ID generator. Zero dependencies.
1ocmpHE1bFnygEBAPTzMK4
1ocmpHE1bFnygFv4Wp4dL2
1ocmpHE1bFnygGoUXUL7XoInstall
npm install sparkidUsage
import { generateId } from "sparkid";
const id = generateId();
// => "1ocmpHE1bFnygEBAPTzMK4"Properties
| Property | Value |
|---|---|
| Length | 22 characters, fixed |
| Alphabet | Base58 (no 0, O, I, l) |
| Sortable | Lexicographically, by creation time |
| Monotonic | Strictly increasing within the process |
| URL-safe | Yes |
| Collision resistance | ~58^14 (~1.8 x 10^24) combinations per millisecond |
| Randomness | Cryptographically secure (crypto.getRandomValues) |
How it works
Each ID is composed of two parts:
[8-char timestamp][14-char suffix]- Timestamp (8 chars): Current time in milliseconds, Base58-encoded. IDs generated in a later millisecond always sort after earlier ones.
- Suffix (14 chars): Seeded from
crypto.getRandomValues(rejection-sampled, no modulo bias) at the start of each millisecond, then monotonically incremented for each subsequent ID within that millisecond. This guarantees strict ordering even when multiple IDs share a timestamp.
Ordering guarantees
All IDs generated by generateId() within a single process are strictly monotonically increasing — every ID is lexicographically greater than the one before it. Since JavaScript is single-threaded, this means process-wide monotonicity with no additional coordination needed.
Performance
~22 million IDs/sec on Node.js (~0.05 µs/call):
npm run benchPlatform support
Works in any environment with crypto.getRandomValues:
- Node.js >= 19
- All modern browsers
- Deno
- Bun
- Cloudflare Workers
License
MIT
