Hash any object based on its value
npm install hash-itFast and consistent hashCode for any object type
- hash-it
- Table of contents
- Usage
- Overview
- Hash consistency
- Support
- Browsers
- Node
- Development
``javascript
// ES2015
import { hash } from 'hash-it';
// CommonJS
const { hash } = require('hash-it');
// hash any standard object
console.log(hash({ foo: 'bar' })); // 13729774857125
// or a circular object
console.log(hash(window)); // 3270731309314
`
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:
- ArgumentsArray
- ArrayBuffer
- AsyncFunction
- (based on toString)AsyncGeneratorFunction
- (based on toString)BigInt
- BigInt64Array
- BigUint64Array
- Boolean
- DataView
- (based on its buffer)Date
- (based on getTime)DocumentFragment
- (based on outerHTML of all children)Error
- (based on stack)TypeError
- Includes all sub-types (e.g., , ReferenceError, etc.)Event
- (based on all properties other than Event.timeStamp)MouseEvent
- Includes all sub-types (e.g., , KeyboardEvent, etc.)Float32Array
- Float64Array
- Function
- (based on toString)GeneratorFunction
- (based on toString)Int8Array
- Int16Array
- Int32Array
- HTMLElement
- (based on outerHTML)HTMLAnchorElement
- Includes all sub-types (e.g., , HTMLDivElement, etc.)Map
- (order-agnostic)Null
- Number
- Object
- (handles circular objects, order-agnostic)Proxy
- RegExp
- Set
- (order-agnostic)SharedArrayBuffer
- String
- SVGElement
- (based on outerHTML)SVGRectElement
- Includes all sub-types (e.g., , SVGPolygonElement, etc.)Symbol
- (based on toString)Uint8Array
- Uint8ClampedArray
- Uint16Array
- Uint32Array
- Undefined
- Window
-
_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.
- PromiseBlob
- There is no way to synchronously obtain the values contained within due to its asynchronous nature
- Promise
- Like , there is no way to synchronously obtain the values contained withinGenerator
- (the result of calling a GeneratorFunction)Promise
- Like , there is no way to obtain the values contained within due to its dynamic iterable natureWeakMap
- / WeakRef / WeakSet
- The spec explicitly forbids iteration over them, so the unique values cannot be discovered
`ts
const promise = Promise.resolve(123);
console.log(hash(promise)); // 16843037491939
console.log(hash(promise)); // 16843037491939
console.log(hash(Promise.resolve(123))); // 4622327363876
`
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.
- Chrome (all versions)
- Firefox (all versions)
- Edge (all versions)
- Opera 15+
- IE 9+
- Safari 6+
- iOS 8+
- Android 4+
- 4+
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 typings