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 🙏

© 2025 – Pkg Stats / Ryan Hefner

distid

v1.0.2

Published

Distributed ID generator for large-scale systems

Readme

DistId

npm license downloads

A lightweight, distributed ID generator designed for large-scale systems. DistId generates unique 64-bit IDs with built-in timestamp, node identifier, and counter, making it ideal for distributed environments where collision resistance and indexing performance are critical.

Why DistId?

  • Efficient Indexing: IDs are time-ordered, reducing fragmentation in database indexes (e.g., B-tree).
  • Scalable: Supports up to 1024 nodes, generating millions of IDs per second.
  • Compact: 64-bit IDs (8 bytes), smaller than UUID (16 bytes) or CUID (25+ bytes).
  • Flexible Formats: Output as number, hex, base36, or base62.
  • TypeScript Support: Fully typed with TypeScript for a better developer experience.

Features

  • Generates 64-bit IDs with timestamp (41 bits), nodeId (10 bits), and counter (12 bits, configurable).
  • Collision-resistant across distributed nodes.
  • Supports multiple output formats: number, hex, base36, base62.
  • Customizable epoch, counter bits, and logging.
  • Lightweight with zero dependencies.

Installation

Install DistId via npm:

npm install distid

Usage

Here's a quick example to get started:

import { DistIdGenerator } from 'distid';

// Initialize with a nodeId (0-1023)
const gen = new DistIdGenerator({ nodeId: 5 });

// Generate IDs in different formats
console.log(gen.generate('hex'));    // e.g., "13a8f5c00050007b"
console.log(gen.generate('base62')); // e.g., "1Qz5X0K"
console.log(gen.generate('number')); // e.g., 1412894000743680005n

Configuration

You can customize the generator with the following options:

|Option |Description |Default | |:--------------|:---------------------------------------|:-------------| |nodeId |Node identifier (0-1023). |Required | |epoch |Custom epoch timestamp (in ms). |2020-01-01 | |counterBits |Number of bits for the counter (8-20). |12 | |logger |Custom logging function for debugging. |Console logger|

Example with custom configuration:

const gen = new DistIdGenerator({
  nodeId: 5,
  epoch: new Date('2023-01-01').getTime(),
  counterBits: 10,
  logger: (msg) => console.log(`[Custom] ${msg}`),
});

console.log(gen.generate('base36')); // e.g., "1h3k5l0k5"

API Reference

DistIdGenerator

Constructor

new DistIdGenerator(config: DistIdConfig)
  • config: Configuration object (see above).

generate(format?: DistIdFormat): string | bigint

Generates a unique ID in the specified format.

  • format: Output format ('number', 'hex', 'base36', 'base62'). Default: 'number'.
  • Returns: A unique ID as a bigint (for 'number') or string (for other formats).

DistIdGenerator.resetNodeIds(): void Resets the internal node ID tracking (useful for testing).

Performance

DistId is designed for high performance in distributed systems. Here's a quick benchmark compared to UUID and CUID (run on Node.js v18):

|Generator|Size (bytes)|IDs/sec (millions)|Index Fragmentation| |:--------|:-----------|:-----------------|:------------------| |DistId |8 |4.2 |Low (time-ordered) | |UUID v4 |16 |3.8 |High (random) | |CUID |25 |3.5 |Medium |

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -m "Add your feature").
  4. Push to the branch (git push origin feature/your-feature).
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.