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

unique-id-generator-ts

v1.0.3

Published

A high-performance unique ID generator based on Snowflake architecture. Supports up to 4096 unique IDs per millisecond per machine.

Readme

Unique ID Generator

npm build license

A high-performance, timestamp-based 64-bit unique ID generator inspired by Twitter's Snowflake architecture. Suitable for distributed systems, supporting up to 4096 unique IDs per millisecond per machine.

✨ Features

  • ✅ 64-bit globally unique ID generation
  • 🕒 Based on timestamp, with millisecond precision
  • ⚙️ Uses os module to generate unique machine ID
  • ⚡️ 4096 IDs per millisecond per node
  • 🧩 decodeId() support to reverse-engineer timestamp, machineId, sequence
  • 📦 Lightweight, TypeScript-first implementation

📦 Installation

npm install unique-id-generator-ts

NPM package: unique-id-generator-ts

🚀 Usage

import { UniqueIdGenerator } from 'unique-id-generator-ts';

const generator = new UniqueIdGenerator();

const id = generator.generateUniqueId();
console.log(id); // e.g., 1308924728312342528

// Decode the ID
const decoded = generator.decodeId(id);
console.log("Decoded Info:", decoded);

🛠️ Constructor with Custom Epoch

You can optionally pass a custom epoch (in milliseconds) to control the base timestamp used in ID generation.

import { UniqueIdGenerator } from 'unique-id-generator-ts';

// Default epoch (if not passed): 1700000000000
const generator = new UniqueIdGenerator(1700000000000);

📝 Note: The epoch affects the timestamp part of your ID. Use a consistent epoch across services to maintain global uniqueness and sortability.

📤 Output of decodeId(id)

{
  timestamp: 1728012388123,
  machineId: 43,
  sequence: 212
}

🧠 ID Structure: 64-bit Snowflake-Inspired Format

Each ID is a 64-bit integer, structured as follows:

| Bits | Field | Description | | ---- | ---------- | ---------------------------------------- | | 41 | Timestamp | Milliseconds since custom epoch | | 10 | Machine ID | Supports 1024 unique nodes | | 12 | Sequence | Allows 4096 IDs per millisecond per node |

⚠️ Important Note

  • To ensure ID uniqueness and efficient performance, always create only one instance of UniqueIdGenerator per process.
  • Creating multiple instances can lead to duplicated sequence numbers, especially within the same millisecond, and may result in non-unique IDs.

✅ Recommended:

// Instantiate once at app startup
const generator = new UniqueIdGenerator();

// Use the same instance throughout your app
const id = generator.generateUniqueId();

✅ Pros

  • 📏 Compact: Fits in a 64-bit integer (unlike UUIDs)
  • ⏱ Time-ordered: Great for databases or event logs
  • ⚡️ Fast: Uses simple bitwise operations for performance

⚠️ Cons

  • 🧩 Slightly more complex than concatenation
  • 🔐 Requires machine ID coordination (to avoid collisions)
  • 🔁 Sequence limit of 4096 per ms per machine