ksort-id
v1.0.1
Published
A high-performance, K-sortable, zero-dependency ID generator for Node.js.
Maintainers
Readme
ksort-id (High-Performance ID Generator)
A zero-dependency, K-Sortable, and URL-safe random ID generator for Node.js and TypeScript.
Designed to replace uuid and nanoid in all major and minor usages.
Why use this?
The Problem with UUIDs
Standard UUIDs (v4) are completely random. When you insert them into a database (MySQL/PostgreSQL), they scatter across the B-Tree index, causing Index Fragmentation and slowing down insertion speeds as your data grows.
The Solution: K-Sortability
ksort-id generates IDs that are Time-Ordered.
- New IDs > Old IDs (lexicographically).
- Database inserts always happen at the "end" of the index.
- Result: ~50% faster database writes compared to UUIDv4 at scale.
Inspired by Twitter's Snowflake ID generation system, ksort-id provides globally unique, time-ordered IDs that ensure efficient database performance and scalability.
Installation
npm install ksort-idUsage
The library exposes two modes: Sortable (for Databases) and Pure Random (for Tokens).
- Database Keys- Best for Primary Keys, User IDs, and Order ID.
import { sortid } from 'ksort-id';
const id = sortid();
// Output: "001iU5yV9b2n5k1lX0z"
// Structure: [Timestamp (8 chars)] + [Randomness (12 chars)]- Tokens & Short Links- Best for URL Shorteners, API Keys, and Invite Codes.
import { randomid } from 'ksort-id';
const token = randomid(6);
// Output: "aZ91x7"
// Structure: Pure RandomnessAPI Reference
sortid(config?: IdConfig): string
Generates a time-ordered, sortable ID optimized for database operations.
Parameters:
config.length?(number) - Total ID length in characters
Defaults & Constraints:
- Default Length: 20 characters (8 timestamp + 12 random)
- Minimum Length: 9 characters (must be greater than 8 to ensure uniqueness)
Examples:
sortid(); // Default: "001iU5yV9b2n5k1lX0z" (20 chars)
sortid({ length: 25 }); // Custom: 25 character sortable IDrandomid(length?: number): string
Generates a pure random string with no ordering properties.
Parameters:
length?(number) - Total ID length in characters
Defaults & Constraints:
- Default Length: 21 characters
- Minimum Length: 1 character
Examples:
randomid(); // Default: "aZ91x7K2mP8qR4tV0w" (21 chars)
randomid(6); // Short: "aZ91x7" (6 chars)
randomid(50); // Long: 50 character random stringArchitecture
Zero Dependencies: No bloat. 100% Native.
Buffer Pooling: Pre-allocates random bytes to minimize System Calls and Garbage Collection.
Base62 Encoding: Custom integer-to-string implementation optimized for V8.
