HashMap Class for JavaScript

2.4.0 · abandoned · verified Sun Apr 19

The `hashmap` package provides a `HashMap` class for JavaScript, designed to store key/value pairs where keys can be of *any* data type, including objects, numbers, and dates, without the implicit stringification that occurs with plain JavaScript objects. This was a key differentiator before the introduction of native `Map` in ES6. The current stable version is 2.4.0. Given its last update was over eight years ago, the package is effectively abandoned, with no active development or release cadence. It targets both Node.js and browser environments and differentiates itself by allowing complex objects as keys without coercion.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to import `HashMap` using CommonJS, set and retrieve values with various key types (string, number, object), check the map's size, delete entries, and iterate over its contents.

const HashMap = require('hashmap');

// Create a new HashMap instance
const map = new HashMap();

// Basic key-value pair storage
map.set('stringKey', 'Hello World');
map.set(123, 'Numeric Key');

console.log('Value for "stringKey":', map.get('stringKey')); // Expected: Hello World
console.log('Value for 123:', map.get(123)); // Expected: Numeric Key

// Demonstrating non-stringified object keys
const objKey1 = { id: 1 };
const objKey2 = { id: 2 };
map.set(objKey1, 'First object');
map.set(objKey2, 'Second object');

console.log('Value for objKey1:', map.get(objKey1)); // Expected: First object
console.log('Value for objKey2:', map.get(objKey2)); // Expected: Second object

// Get current size
console.log('Map size:', map.size); // Expected: 4

// Delete a key-value pair
map.delete('stringKey');
console.log('Map size after deletion:', map.size); // Expected: 3

// Iterate over key-value pairs
console.log('Map entries:');
map.forEach(function(value, key) {
    console.log(`  Key: ${JSON.stringify(key)}, Value: ${value}`);
});

view raw JSON →