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

snowflake-id-monotonic

v1.0.0

Published

A tiny module to generate monotonic time-based 64-bit unique IDs and decode them back to their creation timestamp, inspired by Twitter Snowflake.

Readme

❄️ Snowflake ID Generator (Monotonic)

npm version License: MIT TypeScript Node.js

A performancent, monotonic Snowflake ID generator for Node.js and TypeScript applications. Inspired by Twitter's original Snowflake algorithm, time-ordered, unique IDs across distributed systems with zero dependencies,

Features

  • Monotonic: IDs are always increasing, even when system clock moves backward
  • Async Support: Non-blocking async version for concurrent environments
  • Timezone Aware: Optional for timezone-specific decoding
  • ID Decoding: Extracts creation timestamp from Snowflake ID base to its offset
  • Resilient: Returns "0" on errors instead of throwing
  • TypeScript Native: Full type definitions included
  • Flexible Configuration: Custom epochs, machine IDs

📦 Installation

npm install snowflake-id-monotonic
# or
yarn add snowflake-id-monotonic
# or
pnpm add snowflake-id-monotonic

Basic Usage

import { Snowflake } from 'snowflake-id-monotonic';

// Create generator with default settings
const snowflake = new Snowflake();

// Generate ID (synchronous - fastest)
const id = snowflake.GenerateID();
console.log(id); // "130303618715750401"

// Generate ID (asynchronous - non-blocking)
const asyncId = await snowflake.GenerateIDAsync();
console.log(asyncId); // "130303618715750402"

// Decode ID back to creation timestamp
const timestamp = snowflake.decodetoTimestamp(id);
console.log(timestamp); // "1745109200000" (epoch milliseconds)

// Format timestamp as Date
console.log(new Date(Number(timestamp)));
// Output: 2025-04-19T10:33:20.000Z

Custom Config

import { Snowflake } from 'snowflake-id-monotonic';

// Create generator with custom settings
const snowflake = new Snowflake({
  mid: 42,                    // Machine ID (0-1023)
  offset: "2025-01-01T00:00:00Z", // Custom epoch (ISO string)
  // offset: new Date("2025-01-01"), // Or Date object
  // offset: 1735689600000,    // Or milliseconds
  // now: () => Date.now(),    // Custom time source
});

const id = snowflake.GenerateID();
console.log(id);

🎯 API Reference

new Snowflake(options?) Creates a new Snowflake ID generator instance.

GenerateID(): string Generates a unique Snowflake ID synchronously. Returns "0" on error.

GenerateIDAsync(): Promise(string) Generates a unique Snowflake ID asynchronously. Returns "0" on error.

decodetoTimestamp(id: string, offset?): string Decodes a Snowflake ID back to its creation timestamp (epoch milliseconds).

Distributed System Setup

// Server 1
const server1 = new Snowflake({ mid: 1 });

// Server 2
const server2 = new Snowflake({ mid: 2 });

// Server 3
const server3 = new Snowflake({ mid: 3 });

// Each generates unique, non-overlapping IDs

Database Integration

import { Snowflake } from 'snowflake-id-monotonic';

const snowflake = new Snowflake({ mid: process.env.MACHINE_ID || 1 });

// Create user with Snowflake ID
async function createUser(userData) {
  const userId = await snowflake.GenerateIDAsync();
  const createdAt = Number(snowflake.decodetoTimestamp(userId));
  
  await db.users.insert({
    id: userId,
    ...userData,
    created_at: new Date(createdAt)
  });
  
  return userId;
}

📈 Performance

Benchmarks (Node.js v24, Acer Aspire 5 WIN11 Core i5-11th Gen 16gb ram 1tb ssd)

| Method | Rate (IDs/sec) | Time per ID | Relative Speed | |--------|----------------|-------------|----------------| | GenerateID() | ~1.2 million | ~0.0008ms | 1.52x faster | | GenerateIDAsync() | ~800,000 | ~0.0012ms | Baseline | | Concurrent (100 instances) | ~340,000 | ~0.0029ms | System-wide |

Performance Characteristics

  1. Synchronous (GenerateID())

    • Best for: Bulk generation, single-threaded workloads
    • Overhead: Minimal, but may block event loop briefly
    • Use when: Generating IDs in loops or batches
  2. Asynchronous (GenerateIDAsync())

    • Best for: High-concurrency, I/O-bound applications
    • Overhead: ~52% slower due to Promise scheduling
    • Use when: In async/await contexts, web servers, real-time systems
  3. Memory Efficiency

    • Minimal memory footprint (< 1MB for 1M IDs)
    • No significant GC pressure
    • Suitable for serverless and memory-constrained environments

🤝 Contributing

Every snowflake matters - and so does every contribution... Whether you're fixing a typo or adding a new feature...

How You Can Help:

  • 🐛 Found a bug? File an issue with the snowflake ID that caused it!
  • 💡 Have an idea? Suggest features that would make your snowfall heavier
  • 📚 Better docs? Help others navigate the blizzard
  • 🔧 Code contribution? Check our Contributing Guide to get started

Fun fact: Why 'snowflake'?

Because a snowflake's shape evolves as it journeys through the air, no two will ever be the same. Even two flakes floating side by side will each be blown through different levels of humidity and vapour to create a shape that is truly unique. -BBC UK

First Time? No Problem!

  • Look for issues tagged good-first-issue ❄️ - perfect for your first contribution to an open source snowstorm!
# Quick start for contributors
git clone https://github.com/piscode/snowflake-id-monotonic.git
cd snowflake-id-monotonic
npm install
npm test

benchmark:
 - path\test> node benchmark.js
 - path\test> node benchmark-batch.js

The Originals

Twitter Engineering (2010) - For inventing the Snowflake concept that powers thousands of applications today. Their brilliant 64-bit timestamp-based design revolutionized distributed ID generation. Dustin Rouillard - For the initial JavaScript implementation that made Snowflakes accessible

Special Thanks To:

  • The Open Source Community - For being the atmosphere where ideas can collide and form beautiful patterns
  • Early Adopters - For braving the first snowfall and reporting the icy patches