anyhash
A stable hash for any JavaScript value. The same data always gives the same hash, whatever the key order.
import anyhash from 'anyhash'
anyhash({ a: 1, b: [2, 3] }) // 'e27fd6f02f9e19bec054683a3adce1a6'
anyhash({ b: [2, 3], a: 1 }) // 'e27fd6f02f9e19bec054683a3adce1a6' same
anyhash(new Map([['k', new Set([1, 2])]])) // Map, Set, Date, typed arrays, classes, cycles...
await anyhash.sha256({ a: 1, b: [2, 3] }) // 'efbd0040...' SHA-256 when it must be cryptographic
anyhash.serialize({ b: 1, a: new Date(0) }) // '{"a":Date(0),"b":1}' what actually got hashed
- Any value: objects, arrays,
Map, Set, Date, RegExp, typed arrays, ArrayBuffer, DataView, Error, class instances, symbols, BigInt, circular references
- Exact about types:
1, '1' and 1n hash differently, and so do 0 / -0, null / undefined, { a: 1 } / new Map([['a', 1]])
- Order-independent where order doesn't matter: object keys, and by default
Map and Set entries. Arrays can be too, with an option
- Fast: a 128-bit MurmurHash3, synchronous, no
crypto needed
- SHA-256 through Web Crypto when you need a cryptographic hash
- Debuggable:
serialize() shows the canonical string behind a hash, so you can see why two hashes differ
- Stable: hash values are pinned in the tests and only change in a major version
- Zero dependencies, ESM + CommonJS, types included, Node 20+/Bun/Deno/browsers/edge
Install
npm install anyhash
Why not object-hash?
|
object-hash |
anyhash |
| Ignores object key order |
✅ |
✅ |
| Map, Set, typed arrays, circular references |
✅ |
✅ |
| Fast non-cryptographic hash |
❌ SHA-1 by default |
✅ 128-bit MurmurHash3 |
| SHA-256 |
✅ via Node's crypto |
✅ via Web Crypto, works in browsers and edge runtimes |
| See the data behind a hash |
algorithm: 'passthrough' |
serialize() |
| Types included |
❌ needs @types/object-hash |
✅ |
| ESM + CommonJS |
CommonJS only |
✅ |
What counts as the same value
Two values hash the same exactly when serialize() gives the same string for both.
| Value |
Hashed by |
Strings, numbers, booleans, BigInt, null, undefined |
Type and value. -0 differs from 0; every NaN is the same |
| Plain objects |
Own enumerable string keys, in any order. { a: undefined } differs from {}. Objects with a null prototype count as plain |
| Arrays |
Items in order (any order with unorderedArrays). Holes count as undefined |
Map / Set |
Entries in any order (insertion order with unorderedMaps: false / unorderedSets: false) |
Date |
The timestamp. All invalid dates are the same |
RegExp |
Source and flags (not lastIndex) |
Typed arrays, ArrayBuffer, DataView |
Type and the bytes or elements they show (a subarray hashes like a copy) |
Error |
Class, name, message, own enumerable properties (like code), cause, and errors for AggregateError. Not the stack trace |
| Class instances |
Constructor name plus own enumerable properties, or plus toJSON() if the class has one (so URL, Temporal and decimal types hash by value) |
Boxed primitives (new Number(1)) |
Type and value, different from the primitive |
| Symbols |
Description; Symbol.for() symbols by key |
| Functions |
Name and source code |
| Circular references |
Position in the structure, so identical circular shapes hash the same |
Subclasses keep their name: a class Tags extends Set hashes differently from a Set, and a Buffer differently from a Uint8Array.
Values from another realm (an iframe, vm, Jest's environment) hash the same as local ones.
API
anyhash(value, options?)
Returns a 32-character hex string: a 128-bit MurmurHash3. Synchronous and fast, but not cryptographic: use sha256 if someone could choose inputs to make hashes collide on purpose.
anyhash.hash and the named export hash are the same function.
anyhash.sha256(value, options?)
Returns a Promise of a 64-character hex SHA-256, computed with Web Crypto (crypto.subtle), which Node 20+, Bun, Deno, browsers (on HTTPS pages) and edge runtimes all have.
It is the SHA-256 of the UTF-8 bytes of serialize(value), so you can check it with any SHA-256 tool:
await anyhash.sha256(value) === createHash('sha256').update(anyhash.serialize(value)).digest('hex')
anyhash.serialize(value, options?)
Returns the canonical string that both hashes are computed from. Useful to see why two values hash differently:
anyhash.serialize({ name: 'Ada', tags: new Set(['b', 'a']), joined: new Date(0) })
// '{"joined":Date(0),"name":"Ada","tags":Set["a","b"]}'
class User { constructor() { this.id = 1; this.name = 'Ada' } }
anyhash.serialize(new User())
// '@"User"{"id":1,"name":"Ada"}'
The format is meant for reading and comparing, not for parsing back.
Options
All three functions take the same options.
anyhash(order, { ignoreKeys: ['updatedAt', '_id'] })
anyhash(tags, { unorderedArrays: true })
| Option |
Default |
|
ignoreKeys |
none |
Keys to leave out, at every level. A list of names, or a function (key, value) => boolean that returns true for keys to skip. Applies to object, class instance and Error properties, not to Map keys |
unorderedArrays |
false |
[1, 2] and [2, 1] hash the same |
unorderedSets |
true |
false makes Set insertion order count |
unorderedMaps |
true |
false makes Map insertion order count |
The hash, exactly
anyhash(value) is MurmurHash3 x86 128-bit, seed 0, of the UTF-16LE bytes of 'anyhash1' + serialize(value), written as the four 32-bit words h1 h2 h3 h4 in hex. The implementation is checked against the reference algorithm's SMHasher verification value in the tests.
128 bits makes accidental collisions practically impossible: you would need around 2^64 different values before two are likely to share a hash.
Things to know
- Symbol-keyed and non-enumerable properties are not included, and neither are private class fields (
#x) or getters on the prototype.
WeakMap, WeakSet, WeakRef and Promise can't be inspected, so they hash by class name only: all WeakMaps hash the same.
- The same object can appear in several places; it's hashed in full each time, like
JSON.stringify does.
CommonJS
const anyhash = require('anyhash')
anyhash({ a: 1 })
await anyhash.sha256({ a: 1 })
License
MIT