HashMap Class for JavaScript
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
-
TypeError: HashMap is not a constructor
cause Attempting to use `import HashMap from 'hashmap';` or `import { HashMap } from 'hashmap';` in a pure ES Modules environment where the package only exports CommonJS.fixUse the CommonJS `require` syntax: `const HashMap = require('hashmap');`. -
TypeError: map.remove is not a function
cause Calling the deprecated `remove()` method.fixUse `map.delete(key)` instead of `map.remove(key)`. -
TypeError: Cannot read properties of undefined (reading 'set')
cause Incorrect import or `require` leading to `map` being `undefined`.fixEnsure `const HashMap = require('hashmap');` correctly assigns the constructor and that `new HashMap()` is called to instantiate the map before attempting to use its methods.
Warnings
- gotcha The `hashmap` package has not been updated in over eight years (last publish date is 8 years ago as of April 2026). This indicates it is likely abandoned, may not be compatible with newer Node.js versions or browser features, and will not receive security patches or bug fixes.
- deprecated The methods `remove(key)` and `count()` are deprecated aliases. `remove(key)` is an alias for `delete(key)`, and `count()` is an alias for the `size` property.
- gotcha This package is designed for CommonJS (`require`) and does not natively support ES Modules (`import`). Attempting to use `import` syntax directly in a pure ESM environment without a CJS interop layer will fail.
- gotcha The package does not ship with TypeScript type definitions (`.d.ts` files). Users in TypeScript projects will need to provide their own type declarations or install community-maintained types (if available) to avoid type errors.
Install
-
npm install hashmap -
yarn add hashmap -
pnpm add hashmap
Imports
- HashMap
import { HashMap } from 'hashmap';const HashMap = require('hashmap');
Quickstart
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}`);
});