A high-performance, K-sortable, zero-dependency ID generator for Node.js.
npm install ksort-idA 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.



ksort-id generates IDs that are Time-Ordered.Inspired by Twitter's Snowflake ID generation system, ksort-id provides globally unique, time-ordered IDs that ensure efficient database performance and scalability.
---
``bash`
npm install ksort-id
1. Database Keys-
Best for Primary Keys, User IDs, and Order ID.
`TypeScript
import { sortid } from 'ksort-id';
const id = sortid();
// Output: "001iU5yV9b2n5k1lX0z"
// Structure: [Timestamp (8 chars)] + [Randomness (12 chars)]
`
2. Tokens & Short Links-
Best for URL Shorteners, API Keys, and Invite Codes.
`TypeScript
import { randomid } from 'ksort-id';
const token = randomid(6);
// Output: "aZ91x7"
// Structure: Pure Randomness
`
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:
`TypeScript`
sortid(); // Default: "001iU5yV9b2n5k1lX0z" (20 chars)
sortid({ length: 25 }); // Custom: 25 character sortable ID
Parameters:
- length? (number) - Total ID length in characters
Defaults & Constraints:
- Default Length: 21 characters
- Minimum Length: 1 character
Examples:
`TypeScript``
randomid(); // Default: "aZ91x7K2mP8qR4tV0w" (21 chars)
randomid(6); // Short: "aZ91x7" (6 chars)
randomid(50); // Long: 50 character random string
- Buffer Pooling: Pre-allocates random bytes to minimize System Calls and Garbage Collection.
- Base62 Encoding: Custom integer-to-string implementation optimized for V8.