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

hashvector

v1.0.5

Published

Lightweight library for consistent hashing

Readme

HashVector

npm version License: MIT CI

A high-performance, type-safe consistent hashing library for Node.js and TypeScript. HashVector provides consistent hashing with virtual nodes, supporting multiple hashing algorithms and offering excellent key distribution across shards.

Features

  • 🚀 High Performance: Optimized for fast lookups and minimal memory overhead
  • 🔄 Consistent Hashing: Evenly distributes keys across shards
  • Virtual Nodes: Configurable number of virtual nodes for better distribution
  • 🔒 Multiple Hash Algorithms: Supports MD5, SHA-1, SHA-256, and SHA-512
  • 📦 TypeScript Support: Full type definitions included
  • 🎯 Minimal Dependencies: Lightweight with no unnecessary dependencies
  • 📊 Minimum Key Movement: Minimizes key movement on node addition and removal by using virtual nodes
  • 📊 Key Movement Tracking: Tracks key movement on node addition and removal

Installation

npm install hashvector
#or
pnpm install hashvector
#or
yarn add hashvector

Usage

Basic Usage

import ConsistentHashRing from 'hashvector';

// Create a new consistent hash ring
const ring = new ConsistentHashRing(
  ['shard1', 'shard2', 'shard3', 'shard4', 'shard5', 'shard6', 'shard7', 'shard8', 'shard9', 'shard10', 'shard11', 'shard12', 'shard13', 'shard14', 'shard15', 'shard16', 'shard17', 'shard18', 'shard19', 'shard20'], // List of shards
  160,                            // Number of virtual nodes per shard
  'sha256'                        // Hashing algorithm (optional, default: 'sha256')
);

// Add a node
ring.addNode('shard21');

// Remove a node
ring.removeNode('shard1');

// Get the shard for a key
const shard = ring.get('user123');
console.log(`Key 'user123' is assigned to ${shard}`);

Getting Key Distribution

// Generate test keys
const keys: string[] = [];
for (let i = 0; i < 1_000_000; i++) {
    keys.push(`user${i}`);
}

// Get shard distribution
const distribution = ring.getKeyDistribution(keys);

console.log("Shard Distribution (based on 1,000,000 keys):\n");
for (const [shard, stats] of Object.entries(distribution)) {
    console.log(`${shard}: ${stats.count} keys (${stats.percent})`);
}

API

new ConsistentHashRing(nodes: string[], virtualNodes = 135, algorithm: 'md5' | 'sha1' | 'sha256' | 'sha512' = 'sha256')

Creates a new consistent hash ring.

  • nodes: Array of node names (shards)
  • virtualNodes: Number of virtual nodes per shard (default: 135)
  • algorithm: Hashing algorithm to use (default: 'sha256')

Methods

addNode(node: string): void

Adds a new node to the hash ring.

removeNode(node: string): void

Removes a node from the hash ring.

get(key: string): string | undefined

Gets the node for a given key.

getKeyDistribution(keys: string[] = []): Record<string, { count: number; percent: string }>

Gets the distribution of keys across nodes.

trackKeyMovementOnAddNode(node: string, keys: string[]): { movement: { [key: string]: { oldNode: string, newNode: string } }, changedKeys: string[], totalMovedKeys: number, percentMoved: number }

Tracks key movement on node addition.

trackKeyMovementOnRemoveNode(node: string, keys: string[]): { movement: { [key: string]: { oldNode: string, newNode: string } }, changedKeys: string[], totalMovedKeys: number, percentMoved: number }

Tracks key movement on node removal.

Performance

HashVector is designed for high performance with O(log n) lookup time, where n is the number of virtual nodes in the ring.

License

MIT © [Mathura kumar]

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.