What is hash-it?
The hash-it npm package is a utility for creating unique hash values from JavaScript objects. It is particularly useful for creating hash keys for complex objects, ensuring that the same object will always produce the same hash value.
What are hash-it's main functionalities?
Hashing Objects
This feature allows you to generate a unique hash value for a given JavaScript object. The same object will always produce the same hash value, which is useful for caching or quick comparisons.
const { hash } = require('hash-it');
const obj = { a: 1, b: 2 };
const hashValue = hash(obj);
console.log(hashValue);
Hashing Arrays
This feature allows you to generate a unique hash value for an array. It ensures that the same array will always produce the same hash value, which can be useful for array comparisons or caching.
const { hash } = require('hash-it');
const arr = [1, 2, 3];
const hashValue = hash(arr);
console.log(hashValue);
Hashing Nested Structures
This feature allows you to generate a unique hash value for nested structures, including objects within objects and arrays within objects. It ensures consistency in hash values for complex data structures.
const { hash } = require('hash-it');
const nestedObj = { a: { b: 2 }, c: [1, 2, 3] };
const hashValue = hash(nestedObj);
console.log(hashValue);
Other packages similar to hash-it
object-hash
The object-hash package provides similar functionality to hash-it by generating unique hash values for JavaScript objects. It supports a variety of hashing algorithms and can handle complex nested structures. Compared to hash-it, object-hash offers more customization options for the hashing process.
hasha
The hasha package is a versatile hashing utility that supports multiple hashing algorithms like MD5, SHA-1, and SHA-256. While it is not specifically designed for hashing objects, it can be used to hash strings, buffers, and other data types. It is more general-purpose compared to hash-it.
crypto
The built-in Node.js crypto module provides a wide range of cryptographic functionalities, including hashing. While it is more complex and requires more setup to hash objects compared to hash-it, it offers extensive options for cryptographic operations and is highly customizable.
hash-it
Fast and consistent hashCode for any object type
Table of contents
Usage
import hash from 'hash-it';
const hash = require('hash-it');
console.log(hash({ foo: 'bar' }));
console.log(hash(window));
Overview
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.
With no exceptions?
Well ... sadly, no, there are a few exceptions.
Promise
- There is no way to obtain the values contained within due to its asynchronous nature
Generator
(the result of calling a GeneratorFunction
)
- Like
Promise
, there is no way to obtain the values contained within due to its dynamic iterable nature
WeakMap
/ WeakSet
- The spec explicitly forbids iteration over them, so the unique values cannot be discovered
In each of these cases, no matter what the values of the object, they will always yield the same hash result, which is unique to each object type. If you have any ideas about how these can be uniquely hashed, they are welcome!
Here is the list of object classes that produce unique hashes:
Arguments
Array
ArrayBuffer
AsyncFunction
(based on toString
)BigInt
Boolean
DataView
(based on its buffer
)Date
(based on getTime
)DocumentFragment
(based on outerHTML
of all children
)Error
(based on stack
)
- Includes all sub-types (e.g.,
TypeError
, ReferenceError
, etc.)
Event
(based on all properties other than Event.timeStamp
)
- Includes all sub-types (e.g.,
MouseEvent
, KeyboardEvent
, etc.)
Float32Array
Float64Array
Function
(based on toString
)GeneratorFunction
(based on toString
)Int8Array
Int16Array
Int32Array
HTMLElement
(based on outerHTML
)
- Includes all sub-types (e.g.,
HTMLAnchorElement
, HTMLDivElement
, etc.)
Map
(order-agnostic)Null
Number
Object
(handles circular objects, order-agnostic)Proxy
RegExp
Set
(order-agnostic)String
SVGElement
(based on outerHTML
)
- Includes all sub-types (e.g.,
SVGRectElement
, SVGPolygonElement
, etc.)
Symbol
(based on toString
)Uint8Array
Uint8ClampedArray
Uint16Array
Uint32Array
Undefined
Window
If there is an object class or data type that is missing, please submit an issue.
Equality methods
hash.is
hash.is(value1: any, value2: any): boolean
Compares the two objects to determine equality.
console.log(hash.is(null, 123));
console.log(hash.is(null, null));
hash.is.all
hash.is.all(value1: any, value2: any[, value3: any[, ...valueN]]): boolean
Compares the first object to all other objects passed to determine if all are equal based on hashCode
const foo = {
foo: 'bar',
};
const alsoFoo = {
foo: 'bar',
};
const stillFoo = {
foo: 'bar',
};
console.log(hash.is.all(foo, alsoFoo));
console.log(hash.is.all(foo, alsoFoo, stillFoo));
hash.is.any
hash.is.any(value1: any, value2: any[, value3: any[, ...valueN]]): boolean
Compares the first object to all other objects passed to determine if any are equal based on hashCode
const foo = {
foo: 'bar',
};
const alsoFoo = {
foo: 'bar',
};
const nopeBar = {
bar: 'baz',
};
console.log(hash.is.any(foo, alsoFoo));
console.log(hash.is.any(foo, nopeBar));
console.log(hash.is.any(foo, alsoFoo, nopeBar));
hash.is.not
hash.is.not(value1: any, value2: any): boolean
Compares the two objects to determine non-equality.
console.log(hash.is.not(null, 123));
console.log(hash.is.not(null, null));
Note
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.
Support
Browsers
- Chrome (all versions)
- Firefox (all versions)
- Edge (all versions)
- Opera 15+
- IE 9+
- Safari 6+
- iOS 8+
- Android 4+
Node
Development
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 rollup to build ESM, CJS, and UMD filesclean
=> remove files produced from build
scriptdev
=> run webpack dev server to run example app / playgroundlint
=> run ESLint against all files in the src
folderlint:fix
=> run lint
script, automatically applying fixable changesprepublishOnly
=> run typecheck
, lint
, test
, and build
start
=> alias for dev
scripttest
=> run jest test functions with NODE_ENV=test
test:coverage
=> run test
with coverage checkertest:watch
=> run test
with persistent watchertypecheck
=> run tsc
to validate internal typings