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)); // 3557759737121602
hash-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 on toString)AsyncGeneratorFunction (based on toString)BigIntBigInt64ArrayBigUint64ArrayBooleanDataView (based on the bytes within its view window)Date (based on getTime)DocumentFragment (based on outerHTML of all children)Error (based on message and stack)
TypeError, ReferenceError, etc.)Event (based on all properties other than Event.timeStamp and Event.srcElement)
MouseEvent, KeyboardEvent, etc.)Float16ArrayFloat32ArrayFloat64ArrayFunction (based on toString)GeneratorFunction (based on toString)Int8ArrayInt16ArrayInt32ArrayHTMLElement (based on outerHTML)
HTMLAnchorElement, HTMLDivElement, etc.)Map (order-agnostic)NullNumberObject (handles circular objects, order-agnostic)ProxyRegExpSet (order-agnostic)SharedArrayBufferStringSVGElement (based on outerHTML)
SVGRectElement, SVGPolygonElement, etc.)Symbol (based on toString)Uint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayUndefinedWindowAre 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
Blob
Promise, there is no way to synchronously obtain the values contained withinGenerator (the result of calling a GeneratorFunction)
Promise, there is no way to obtain the values contained within due to its dynamic iterable natureWeakMap / WeakRef / WeakSet
const promise = Promise.resolve(123);
console.log(hash(promise)); // 8959449433830577
console.log(hash(promise)); // 8959449433830577
console.log(hash(Promise.resolve(123))); // 2215269628940933
If 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:
0 and -0 produce the same hash, as do two NaN values, using
SameValueZero comparison.undefined, so [, ,] and [undefined, undefined] produce the same hash.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 => run build:es, build:cjs, and build:umd scriptsbuild:cjs => run rollup to build cjs filesbuild:es => run rollup to build es filesbuild:umd => run rollup to build umd filesclean => remove files produced from build scriptclean:cjs => remove files produced from build:cjs scriptclean:es => remove files produced from build:es scriptclean:umd => remove files produced from build:umd scriptdev => run dev server to run example app / playgroundformat => run prettier to format repoformat:check => run prettier to validate formatting in repolint => run ESLint against all files in the src folderlint:fix => run lint script, automatically applying fixable changesrelease:alpha => release a new alpha version under the next tagrelease:beta => release a new beta version under the next tagrelease:rc => release a new rc version under the next tagrelease:stable => release a new stable version under the latest tagstart => alias for dev scripttest => run jest test functions with NODE_ENV=testtypecheck => run tsc to validate internal typingsOverall
Maintained
Code review
npm
Latest release
Releases this year
3,489 followers · starred Jul 2023
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)); // 3557759737121602
hash-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 on toString)AsyncGeneratorFunction (based on toString)BigIntBigInt64ArrayBigUint64ArrayBooleanDataView (based on the bytes within its view window)Date (based on getTime)DocumentFragment (based on outerHTML of all children)Error (based on message and stack)
TypeError, ReferenceError, etc.)Event (based on all properties other than Event.timeStamp and Event.srcElement)
MouseEvent, KeyboardEvent, etc.)Float16ArrayFloat32ArrayFloat64ArrayFunction (based on toString)GeneratorFunction (based on toString)Int8ArrayInt16ArrayInt32ArrayHTMLElement (based on outerHTML)
HTMLAnchorElement, HTMLDivElement, etc.)Map (order-agnostic)NullNumberObject (handles circular objects, order-agnostic)ProxyRegExpSet (order-agnostic)SharedArrayBufferStringSVGElement (based on outerHTML)
SVGRectElement, SVGPolygonElement, etc.)Symbol (based on toString)Uint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayUndefinedWindowAre 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
Blob
Promise, there is no way to synchronously obtain the values contained withinGenerator (the result of calling a GeneratorFunction)
Promise, there is no way to obtain the values contained within due to its dynamic iterable natureWeakMap / WeakRef / WeakSet
const promise = Promise.resolve(123);
console.log(hash(promise)); // 8959449433830577
console.log(hash(promise)); // 8959449433830577
console.log(hash(Promise.resolve(123))); // 2215269628940933
If 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:
0 and -0 produce the same hash, as do two NaN values, using
SameValueZero comparison.undefined, so [, ,] and [undefined, undefined] produce the same hash.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 => run build:es, build:cjs, and build:umd scriptsbuild:cjs => run rollup to build cjs filesbuild:es => run rollup to build es filesbuild:umd => run rollup to build umd filesclean => remove files produced from build scriptclean:cjs => remove files produced from build:cjs scriptclean:es => remove files produced from build:es scriptclean:umd => remove files produced from build:umd scriptdev => run dev server to run example app / playgroundformat => run prettier to format repoformat:check => run prettier to validate formatting in repolint => run ESLint against all files in the src folderlint:fix => run lint script, automatically applying fixable changesrelease:alpha => release a new alpha version under the next tagrelease:beta => release a new beta version under the next tagrelease:rc => release a new rc version under the next tagrelease:stable => release a new stable version under the latest tagstart => alias for dev scripttest => run jest test functions with NODE_ENV=testtypecheck => run tsc to validate internal typingsOverall
Maintained
Code review
npm
Latest release
Releases this year
3,489 followers · starred Jul 2023