Sign inDemoInstall

Package Overview
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies


Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions

Version published



npm version npm downloads Mastodon Follow

[!NOTE] This is one of 193 standalone projects, maintained as part of the monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️


Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions.

Idea based on segmentio/ksuid, though the added flexibility in terms of configuration & implementation also enables the creation of ULIDs:

FeatureKSUID defaultULID default
Configurable bit size160 bits128 bits
Base-N encoding schemebase62(1)base32 (Crockford)
Timestamp resolutionseconds (32 bits)milliseconds (48 bits)
milliseconds (64 bits)
Epoch start time offsetapprox. 2020-09-13(2)none
Time-only base ID generation✅✅
ID parsing / decomposition✅✅
Configurable RNG source(3)✅✅
  • (1) See for alternatives
  • (2) With the default offset, the max. supported date for KSUID32 is 2156-10-20T18:54:55Z
  • (3) Default: window.crypto, Math.random as fallback

IDs generated w/ this package are composed of a 32, 48 or 64 bit Unix epochs and N additional bits of a random payload (from a configurable source). By default all timestamps are shifted to free up bits for the future. IDs can be generated as byte arrays or base-N encoded strings. For the latter, the JS runtime MUST support BigInt.

KSUID bit layout diagram

CLI usage

Since v3.1.0 a small CLI for ad-hoc KSUID generation is included. Currently only KSUID32 is supported and IDs are always based on the current time:

# 0dwncLZE8byaQdccncWDmsNmlYt

# optionally provide number of random bytes to be used (default: 16)
npx 8
# 01ogp9KDpWlQ0pXCY


STABLE - used in production

Search or submit any issues for this package

Breaking changes

Since v3.0.0 all epoch time-shift config values are to be given in milliseconds. This change is unifying this behavior and is only a breaking change if using KSUID32 and specifying custom epoch offsets (using defaults is not impacted). Previously, KSUID32 used an offset given in seconds, whereas the other implementations already used milliseconds.

  • - Arbitrary base-n conversions w/ presets for base8/16/32/36/58/62/64/83/85, support for bigints and encoding/decoding of byte arrays
  • - Generator of opaque numeric identifiers with optional support for ID versioning and efficient re-use
  • - Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation


yarn add

ESM import:

import * as ksuid from "";

Browser ESM import:

<script type="module" src=""></script>

JSDelivr documentation

For Node.js REPL:

const ksuid = await import("");

Package sizes (brotli'd, pre-treeshake): ESM: 990 bytes



Generated API docs

import { defKSUID32, defKSUID64, defULID } from "";

// init 32bit epoch (resolution: seconds) w/ defaults
const id = defKSUID32();
// init 64bit epoch (resolution: milliseconds), same API
const id = defKSUID64();
// init 48bit epoch (resolution: milliseconds), same API
const id = defULID();;
// '05XCWbXa3akRqLDBUw4ogCVKGkd'

const a = id.nextBinary()
// Uint8Array(20) [
//     0, 160,  48, 77, 101, 251,
//   244,  17, 155, 97,  24, 101,
//    70,  71, 207, 23,  32,  21,
//   244, 116
// ]

// format a binary KSUID
// '05XCZ32AaDZfZt0SWE2C22o6cqK'

// {
//   epoch: 1610498125000,
//   id: Uint8Array(16) [
//     101, 251, 244,  17, 155, 97,
//      24, 101,  70,  71, 207, 23,
//      32,  21, 244, 116
//   ]
// }

new Date(1610498125000).toISOString()
// '2021-01-13T00:35:25.000Z'

Creating custom IDs:

import { BASE36 } from "";

// using base36, no time shift, 64bit random part
const id36 = defKSUID32({ base: BASE36, epoch: 0, bytes: 8 });;


Benchmarks can be run via yarn bench. All timings in milliseconds (test config: Node v20.4.0, MBA M1 2021, 16GB). The benchmark collects N KSUIDs w/ different configs in an array, with each case being run 100 times.

b62, 128bit, n=1000010012158.6821.5921.5719.9125.9120.4221.876.26
b62, 64bit, n=1000010011200.4012.0011.9511.2714.6611.8212.103.99


If this project contributes to an academic publication, please cite it as:

  title = "",
  author = "Karsten Schmidt",
  note = "",
  year = 2020


© 2020 - 2024 Karsten Schmidt // Apache License 2.0



Last updated on 21 Jun 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc