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 🙏

© 2024 – Pkg Stats / Ryan Hefner

consistent-hashring

v0.0.1

Published

A simple, pure JS implementation of consistent hashing

Downloads

43

Readme

consistent-hashring

A simple, pure JS implementation of consistent hashing.

Install

npm install consistent-hashring

Basic Usage

Create a new hashring

const ConsistentHashRing = require('consistent-hashring');
let hashring = new ConsistentHashRing();

Add some nodes

for (let i = 0; i < 10; i++) {
  hashring.addNode('node_' + i);
}

Distribute some keys among the nodes

const nodeForKeyA = hashring.getNode('A');
const nodeForKeyB = hashring.getNode('B');

Remove some nodes, only keys associated with the removed nodes need to be re-hashed:

hashring.removeNode('node_50');
hashring.removeNode('node_90');

Advanced Usage

Weight the nodes

By default, 100 replicas of each node are distributed around the ring. You can change this by passing a replicaCount as the second parameter of addNode:

hashring.addNode('nodeA', 50); //half the default weight
hashring.addNode('nodeB', 200); //twice the default weight

Specify the hashing algorithm

By default, a ConsistentHashRing uses Gary Court's murmurhash3_32_gc implementation for hashing. This is not a perfect hash algorithm; there can be collisions between nodes. However, collisions are infrequent, and the algorithm is relatively fast and distributes well. When using a non-perfect hash function, it's possible for a node's replica to collide with another node's replica during addNode. For simplicity's sake, the particular colliding replica is simply skipped. This allows ConsistentHashRing to not care about collisions, since having 99 vs 100 replicas doesn't matter much in practice. This simple handling of collisions also means that the hashing algorithm you provide should have a low number of collisions. If the number of collisions is too high, it's possible that nodes added earlier get weighted more than nodes added later. You can modify test/test.js and run npm run test to see how well your hash function distributes. You can inject your own hashing algorithm when instantiating a ConsistentHashRing

const myGreatHashFunction = function(str) {
  //return some hash of str
};

let hashring = new ConsistentHashRing(myGreatHashFunction);

License

MIT