Fast and consistent hashCode for any object type
// ES2015
import { hash } from 'hash-it';
// CommonJS
const { hash } = require('hash-it');
// hash any standard object
console.log(hash({ foo: 'bar' })); // 1663244226405536
// or a circular object
console.log(hash(window)); // 3557759737121602hash-it has a simple goal: provide a fast, consistent, unique hashCode for any object type that is uniquely based on
its values. This has a number of uses such as duplication prevention, equality comparisons, blockchain construction,
etc.
Any object type?
Yes, any object type. Primitives, ES2015 classes like Symbol, DOM elements (yes, you can even hash the window object
if you want). Any object type. Here is the list of object classes that produce consistent, unique hashes based on their
value:
ArgumentsArrayArrayBufferAsyncFunction(based ontoString)AsyncGeneratorFunction(based ontoString)BigIntBigInt64ArrayBigUint64ArrayBooleanDataView(based on the bytes within its view window)Date(based ongetTime)DocumentFragment(based onouterHTMLof allchildren)Error(based onmessageandstack)- Includes all sub-types (e.g.,
TypeError,ReferenceError, etc.)
- Includes all sub-types (e.g.,
Event(based on all properties other thanEvent.timeStampandEvent.srcElement)- Includes all sub-types (e.g.,
MouseEvent,KeyboardEvent, etc.)
- Includes all sub-types (e.g.,
Float16ArrayFloat32ArrayFloat64ArrayFunction(based ontoString)GeneratorFunction(based ontoString)Int8ArrayInt16ArrayInt32ArrayHTMLElement(based onouterHTML)- Includes all sub-types (e.g.,
HTMLAnchorElement,HTMLDivElement, etc.)
- Includes all sub-types (e.g.,
Map(order-agnostic)NullNumberObject(handles circular objects, order-agnostic)ProxyRegExpSet(order-agnostic)SharedArrayBufferStringSVGElement(based onouterHTML)- Includes all sub-types (e.g.,
SVGRectElement,SVGPolygonElement, etc.)
- Includes all sub-types (e.g.,
Symbol(based ontoString)Uint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayUndefinedWindow
Are there any exceptions?
Sadly, yes, there are a few scenarios where internal values cannot be introspected for the object. In this case, the object is hashed based on its class type and reference.
Promise- There is no way to synchronously obtain the values contained within due to its asynchronous nature
Blob- Like
Promise, there is no way to synchronously obtain the values contained within
- Like
Generator(the result of calling aGeneratorFunction)- Like
Promise, there is no way to obtain the values contained within due to its dynamic iterable nature
- Like
WeakMap/WeakRef/WeakSet- The spec explicitly forbids iteration over them, so the unique values cannot be discovered
const promise = Promise.resolve(123);
console.log(hash(promise)); // 8959449433830577
console.log(hash(promise)); // 8959449433830577
console.log(hash(Promise.resolve(123))); // 2215269628940933If there is an object class or data type that is missing, please submit an issue.
While the hashes will be consistent when calculated within the same environment, there is no guarantee that the
resulting hash will be the same across different environments due to environment-specific or browser-specific
implementations of features. This is limited to extreme edge cases, such as hashing the window object, but should be
considered if being used with persistence over different environments.
The same applies across versions of hash-it itself: the value produced for a given input may change between releases
as the algorithm is refined. Hashes are intended for comparison within a single running program, not for persistence.
A few specifics are worth calling out:
0and-0produce the same hash, as do twoNaNvalues, usingSameValueZerocomparison.- A hole in a sparse array is treated as
undefined, so[, ,]and[undefined, undefined]produce the same hash. - Enumerable own properties added to an array beyond its indices are included in its hash, on the same terms as a plain object.
An ES2015 environment is required; the published bundles are emitted as ES2015 syntax and use ES2015 built-ins such as
WeakMap.
Clone the repo and dependencies via yarn. The npm scripts available:
benchmark=> run benchmark of various data typesbenchmark:compare=> run benchmark of some data types comparing against other hashing modulesbuild=> runbuild:es,build:cjs, andbuild:umdscriptsbuild:cjs=> run rollup to buildcjsfilesbuild:es=> run rollup to buildesfilesbuild:umd=> run rollup to buildumdfilesclean=> remove files produced frombuildscriptclean:cjs=> remove files produced frombuild:cjsscriptclean:es=> remove files produced frombuild:esscriptclean:umd=> remove files produced frombuild:umdscriptdev=> run dev server to run example app / playgroundformat=> runprettierto format repoformat:check=> runprettierto validate formatting in repolint=> run ESLint against all files in thesrcfolderlint:fix=> runlintscript, automatically applying fixable changesrelease:alpha=> release a newalphaversion under thenexttagrelease:beta=> release a newbetaversion under thenexttagrelease:rc=> release a newrcversion under thenexttagrelease:stable=> release a new stable version under thelatesttagstart=> alias fordevscripttest=> run jest test functions withNODE_ENV=testtypecheck=> runtscto validate internal typings