npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

microshard-uuid

v1.0.0

Published

A zero-lookup, partition-aware UUIDv8 implementation with microsecond precision.

Readme

MicroShard UUID (JavaScript / TypeScript)

A zero-lookup, partition-aware UUIDv8 implementation.

microshard-uuid is a 128-bit identifier compliant with IETF RFC 9562 (UUIDv8). Unlike standard UUIDs (v4/v7), MicroShard embeds a 32-bit Shard ID directly into the ID.

This enables Zero-Lookup Routing: your application can determine the database shard, tenant, or region of a record simply by parsing its Primary Key, without hitting a lookup table or cache.

📦 Features

  • Zero-Lookup Routing: Extract Shard/Tenant IDs instantly (O(1) bitwise op).
  • Chronologically Sortable: 54-bit high-precision timestamp ensures correct ordering.
  • High Performance: Built on BigInt for efficient 64-bit register operations.
  • Comparison API: Native methods for sorting (compare, lt, gt) that handle unsigned 64-bit logic correctly.
  • ISO 8601 Support: Lossless microsecond precision.
  • Universal: Works in Node.js (native crypto) and Browsers (Web Crypto API).
  • Zero Dependencies: Lightweight and fast.

🛠 Installation

npm install microshard-uuid

🚀 Usage

1. Basic Generation & Routing

Generate a UUID object, convert it to a string for the DB, or extract the Shard ID for routing.

const { generate } = require('microshard-uuid');

// 1. Generate an ID for Shard #101
const uid = generate(101);

// 2. Storage (Canonical String)
console.log(uid.toString());
// Output: 018e65c9-3a10-0400-8000-a4f1d3b8e1a1

// 3. Routing (Extract Shard ID)
// No string parsing required; uses internal bitwise logic.
if (uid.getShardId() === 101) {
    console.log("Routing to Shard 101...");
}

2. Parsing (Handling DB Data)

Since microshard-uuid uses a Class to optimize performance, you must parse strings coming from your database to access the helper methods.

const { MicroShardUUID } = require('microshard-uuid');

const dbValue = "018e65c9-3a10-0400-8000-a4f1d3b8e1a1";

// Parse string back into Object
const uid = MicroShardUUID.parse(dbValue);

console.log(uid.getShardId()); // 101
console.log(uid.getDate());    // Date Object

3. Sorting & Comparison (New!)

The library provides optimized comparison methods that correctly handle 128-bit unsigned logic. This is significantly faster and safer than string sorting.

const { generate, MicroShardUUID } = require('microshard-uuid');

const id1 = generate(1);
const id2 = generate(1);

// A. Relational Checks
if (id1.lt(id2)) {
  console.log("id1 is older than id2");
}

// B. Sorting Arrays (Chronological)
const list = [id2, id1, generate(1)];

// Pass the static compare function directly
list.sort(MicroShardUUID.compare);

// list is now sorted: [Oldest, ..., Newest]

4. Extracting Time (Metadata)

You can decode the creation time from any MicroShard UUID.

const uid = generate(1);

// Option A: Get standard JS Date
// Warning: Microseconds are truncated to milliseconds.
const date = uid.getDate();
console.log(date.toISOString()); // 2025-12-12T10:00:00.123Z

// Option B: Get ISO String (Microseconds)
// Preserves full 54-bit precision (e.g., for audit logs).
const iso = uid.getIsoTimestamp();
console.log(iso); // 2025-12-12T10:00:00.123456Z

5. Binary Storage

For databases supporting ByteA or Blob (like Postgres or MySQL), storing raw bytes saves 50% space vs strings.

const bytes = uid.toBytes(); // Returns Uint8Array(16)
// Save 'bytes' to DB...

💻 API Reference

Top-Level Functions

| Function | Description | | :--- | :--- | | generate(shardId) | Generates a new MicroShardUUID using system time. | | fromTimestamp(ts, shardId) | Generates a UUID for a specific past/future time. ts can be Date, ISO string, or BigInt micros. |

Class: MicroShardUUID

Instances returned by generate or parse have the following methods:

| Method | Returns | Description | | :--- | :--- | :--- | | .toString() | string | Canonical 8-4-4-4-12 UUID string. | | .toBytes() | Uint8Array | Raw 16-byte binary representation. | | .getShardId() | number | Extracts the 32-bit Shard/Tenant ID. | | .getDate() | Date | Extracts creation time (ms precision). | | .getIsoTimestamp()| string | Extracts time as ISO 8601 string (us precision). | | .compare(other) | -1, 0, 1 | Compare two UUIDs (for sorting). | | .lt(other) | boolean | Less Than. | | .gt(other) | boolean | Greater Than. | | .equals(other) | boolean | Equality check. |

Static Methods

| Method | Description | | :--- | :--- | | MicroShardUUID.parse(str) | Validates and parses a string into a MicroShardUUID. | | MicroShardUUID.compare(a, b) | Helper for Array.sort(). |


📐 Specification (54/32/36)

Total Size: 128 Bits

| Component | Bits | Description | | :--- | :--- | :--- | | Time | 54 | Unix Microseconds (Valid until Year 2541) | | Ver | 4 | Fixed (Version 8) | | Shard | 32 | 4.29 Billion Shards / Tenants | | Var | 2 | Fixed (Variant 2) | | Random | 36 | 68.7 Billion random slots per microsecond |


🌐 Browser Support

This library automatically detects the environment (Zero-Config).

  • Node.js: Uses crypto.randomBytes and process.hrtime.
  • Browser/Edge: Uses window.crypto and performance.now.

Note: BigInt support is required (Available in all modern browsers and Node 10+).