# hashmap

> HashMap Class for JavaScript

Latest version **3.0.1** (published 2026-08-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install hashmap
pnpm add hashmap
yarn add hashmap
bun add hashmap
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2026-08-13 |
| First published | 2012-06-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 0 |
| Unpacked size | 26.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 384 |
| Author | Ariel Flesler |
| Maintainers | flesler |
| Keywords | hashmap, map, object, array, associative, javascript, nodejs, node, browser |

## Links

- npm: https://www.npmjs.com/package/hashmap
- Repository: https://github.com/flesler/hashmap
- Issues: https://github.com/flesler/hashmap/issues
- npm.io page: https://npm.io/package/hashmap

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2026-08-13
- 3.0.0 — 2026-08-06
- 2.4.0 — 2019-10-03
- 2.3.0 — 2017-08-15
- 2.2.0 — 2017-08-14
- 2.1.0 — 2017-04-07
- 2.0.6 — 2016-05-23
- 2.0.5 — 2016-04-08
- 2.0.4 — 2015-10-14
- 2.0.3 — 2015-04-10
- 2.0.2 — 2015-04-04
- 2.0.1 — 2015-02-10
- 2.0.0 — 2014-12-17
- 1.2.0 — 2014-11-25
- 1.1.0 — 2014-06-14
- … 6 more at https://npm.io/package/hashmap/versions

## README

# hashmap

A small `HashMap` class for JavaScript — any key type, stable API since 2012, rewritten for 3.0.

Works in Node.js (14+) and the browser. ESM, CommonJS, TypeScript types, and an IIFE build included.

[![NPM](https://nodei.co/npm/hashmap.png?compact=true)](https://npmjs.org/package/hashmap)

```bash
npm install hashmap
```

```js
import HashMap from 'hashmap'
// or
const HashMap = require('hashmap')
```

---

## Why this exists

Native `Map` didn't exist when this library was created. It does now — and for most new code, **`Map` is the right default**.

`hashmap` is still worth reaching for when you need more than `Map` gives you:

| | `hashmap` | `Map` | `WeakMap` |
|---|---|---|---|
| Any key type (`null`, numbers, strings, objects, …) | yes | yes | objects only |
| Object keys by identity | yes | yes | yes |
| Iterate all entries / `.size` | yes | yes | no |
| Value-based equality for `Date`, `RegExp`, arrays | yes | no | no |
| Reverse lookup (`.search(value)`) | yes | no | no |
| Method chaining on mutators | yes | partial | no |

**Use `hashmap` when:**

- You need **custom key equality** — two `Date` objects with the same timestamp, two `/foo/` regexes, arrays compared element-wise.
- You need **`.search()`** — find the key for a value.
- You're maintaining **legacy code** that already depends on this API (plugin systems, protobuf registries, object-key pools, etc.).
- You want a **stable, zero-dependency** map that behaves the same in Node and the browser.

**Use native `Map` when:** string/number/symbol keys with `===` equality is enough. That's most apps.

**Use `WeakMap` when:** keys are objects and you want them garbage-collected when nothing else references them. You can't iterate a `WeakMap`.

---

## Quick start

```js
const map = new HashMap()

map.set('1', 'string one')
map.set(1, 'number one')
map.get('1') // 'string one'
map.get(1)   // 'number one'

const a = {}
const b = {}
map.set(a, 'first')
map.set(b, 'second')
map.get(a) // 'first' — object identity, not stringification
```

### Method chaining

```js
new HashMap()
  .set(1, 'one')
  .set(2, 'two')
  .forEach((value, key) => console.log(key, value))
```

### Iteration

```js
map.forEach((value, key) => { /* … */ })

for (const { key, value } of map) {
  console.log(key, value)
}
```

### Reverse lookup

```js
map.set('id', 42)
map.search(42) // 'id'
```

---

## API

### Constructor

- `new HashMap()` — empty map
- `new HashMap(other)` — copy from another `HashMap`
- `new HashMap([['k1', 'v1'], ['k2', 'v2']])` — from pairs array
- `new HashMap(k1, v1, k2, v2, …)` — from arguments

### Methods

| Method | Description |
|---|---|
| `get(key)` | Value for `key`, or `undefined` |
| `set(key, value)` | Store a pair (chainable) |
| `has(key)` | Whether `key` exists |
| `delete(key)` | Remove by key (chainable) |
| `search(value)` | Key for `value`, or `null` |
| `clear()` | Remove all entries (chainable) |
| `forEach(fn, ctx?)` | Iterate `(value, key)` (chainable) |
| `keys()` / `values()` / `entries()` | Arrays of keys, values, or `[key, value]` pairs |
| `clone()` | Shallow copy as a new `HashMap` |
| `copy(other)` | Copy entries from `other` into this map (chainable) |
| `multi(k1, v1, …)` | Set several pairs at once (chainable) |
| `size` | Number of entries |

Deprecated aliases kept for compatibility: `remove()` → `delete()`, `count()` → `size`, `type()`.

### TypeScript

Types ship with the package. Generics work as expected:

```ts
import HashMap from 'hashmap'

const cache = new HashMap<string, number>()
cache.set('answer', 42)
```

### Upgrading from 2.x / `@types/hashmap`

Types ship with the package since **3.0.0** — remove DefinitelyTyped:

```bash
npm uninstall @types/hashmap
```

`@types/hashmap` used `export = HashMap` (CommonJS assignment import). 3.0 uses a default export:

```ts
import HashMap from 'hashmap'
// import HashMap = require('hashmap')  // still works in TS with esModuleInterop
```

`hash()` was removed (internal helper, no known dependents). Deprecated `remove()`, `count()`, and `type()` remain. See [docs/backwards-compatibility.md](docs/backwards-compatibility.md).

---

## Browser

For a script tag without a bundler, use the IIFE build:

```html
<script src="node_modules/hashmap/dist/hashmap.iife.js"></script>
<script>
  const map = new HashMap()
  map.set(document.body, 'root')
</script>
```

---

## Benchmarks

3.0 rewrote the internals (numeric hashing, flat entry storage, eager `Map` index, cached entry hashes). Measured on Node 24, ~250 ms per benchmark, string keys unless noted. Run locally:

```bash
npm run benchmark
npm run benchmark -- -o tmp/current.json --compare tmp/benchmark-baseline.json
```

### vs 2.4.0

| Operation | 2.4.0 | 3.0.0 | Change |
|---|---:|---:|---:|
| `set` | 226K | 266K | **1.2×** |
| `get` | 224K | 298K | **1.3×** |
| `has` | 237K | 268K | **1.1×** |
| `delete` | 210K | 264K | **1.3×** |
| `set` (replace existing key) | 7K | 193K | **26×** |
| `get` (map with 1024 entries) | 10K | 273K | **27×** |
| `forEach` (1024 entries) | 29K | 1.7M | **60×** |
| `keys` (1024 entries) | 26K | 722K | **28×** |
| `clone` (1024 entries) | 11K | 19K | **1.7×** |
| `copy` (1024 entries) | 12K | 19K | **1.6×** |
| `set` after `delete` (1024 entries) | 238K | 91K | 0.4× |

**Summary:** 18 of 20 benchmarks faster than 2.4.0. Biggest wins: replacing/updating keys, iteration, and clone/copy. Remaining gaps: burst inserts into an empty map and delete-then-set on large maps.

Values are ops/sec (higher is better). Native `Map` is faster on plain `get` with string keys — `hashmap` trades that for custom equality, `.search()`, and the legacy API.

---

## Development

```bash
npm test        # types + unit tests
npm run build   # dist (esm, cjs, iife, dts)
npm run lint
```

## License

[MIT](LICENSE) © [Ariel Flesler](https://github.com/flesler)

---
_Source: https://npm.io/package/hashmap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
