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

simple-hashtable

v1.0.1

Published

Javascript implementation of a simple hash table data structure

Downloads

636

Readme

npm version license

Simple Hash Table

A javascript implementation of a hash table data structure.

Description

A hash table (or hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute the index into an array of buckets, or slots, from which the correct value can be found. This 'hashing' of the key makes a lookup very efficient and independent of the number of items in the hash table.

This implementation uses the djb2 hash function which provides a good distribution of hashed values (or indexes) to minimize collisions. A hash collision occurs when two different keys hash to the same value. If the collisions are not handled properly, the first value in the hash table will be overwritten by the second.

Hash collisions can be handled using one of several techniques. One way is to implement what is called separate chaining. With separate chaining, instead of assigning a single value to the index (or hash), some type of additional data structure is assigned, say a linked-list for example. Then the payload (key/value pair) is added to the data structure based on the data structure's native API. So basically, the hash table becomes an array of data structures--a data structure of data structures.

This project implements separate chaining to mitigate hash collisions.

Another method for resolving hash collisions is linear probing. Linear probing is not implemented in this project.

For specific examples and documentation, see the below sections

Environment:

Although this implementation is designed to be used with Node.js, it could be used in other contexts with minor modifications. This implementation does not have any external dependencies that would preclude it from being used in the browser--just include it with a <script> tag and it should be good to go. Disclaimer: I have not tested this implementation in any other context/environment; only tested with node.js


Basic Usage

Install with npm :

npm install simple-hashtable --save
var SimpleHashTable = require('simple-hashtable');
var hashtable = new SimpleHashTable();

hashtable.isEmpty();
// --> true

hashtable
    .put('node', 'asynchronous, event-driven io for server side javascript')
    .put('mongodb', 'noSQL database');
// API supports method chaining for 'put' method

hashtable.isEmpty();
// --> false

hashtable.size();
// --> 2

hashtable.containsKey('node');
// --> true

hashtable.containsKey('express');
// --> false

hashtable.containsValue('noSQL database');
// --> true;

hashtable.get('node');
// --> asynchronous, event-driven io for server side javascript

hashtable.get('mongodb');
// --> noSQL database

hashtable.keys();
// --> ['node', 'mongodb']

hashtable.values();
// --> ['asynchronous, event-driven io for server side javascript',
//      'noSQL database']

hashtable.put('node', 'server side javascript');
// overwrites old value with new value

hashtable.get('node');
// --> server side javascript

hashtable.remove('mongodb');
// --> true

hashtable.get('mongodb');
// --> -1

hashtable.size();
// --> 1

hashtable.clear();
hashtable.isEmpty();
// --> true

API

Available methods for a Hash Table instance:

  • isEmpty()

    Determines if the hash table is empty or not. Returns true if is empty, false otherwise.

  • clear()

    Clears all entries in the hash table

  • size()

    Returns the number of keys in the hash table

  • put(key, value)

    Puts the value in the hash table and utilizes the key for lookup

  • get(key)

    Gets the value from the hash table that is associated with the key

  • remove(key)

    Removes the value from the hash table that is associated with the key

  • containsKey(key)

    Determines whether or not the hash table contains the key

  • containsValue(value)

    Determines whether or not the hash table contains the value

  • keys()

    Returns an array of all the keys in the hash table

  • values()

    Returns an array of all the values in the hash table

  • setHashFn(fn)

    Sets the hash function for the hash table to fn


License

MIT © Jason Jones