| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Tiny and fast node.js object hash library with properties/arrays sorting to provide constant hashes. It also provides a method that returns sorted object strings that can be used for object comparison without hashes. One of the fastest among other analogues (see benchmarks).
Hashes are built on top of node's crypto module. If you want to use it in browser it's recommented to use objectSorter only. It will provide you with unique string representation of your object. Afterwards you may use some hash library to reduce string size. Also you may use something like browserify-crypto or some kind of crypto functions polyfills.
Disclaimer: No new features or changes that may break hashes from previous versions. There's no need to update unless you're starting project from scratch.
const hasher = require('node-object-hash')({coerce: {set: true, symbol: true}})
npm i node-object-hash -S
This map displays what types will have identical string representation (e.g. new Set([1, 2, 3]) and [1, 2, 3] will have equal string representations and hashes.
| Initial type | Mapped type |
|---|---|
| Array ([]) | array |
| ArrayObject (new Array()) | |
| Int8Array | |
| Uint8Array | |
| Uint8ClampedArray | |
| Int16Array | |
| Uint16Array | |
| Int32Array | |
| Uint32Array | |
| Float32Array | |
| Float64Array | |
| Buffer | |
| Set | |
| Map | array[array] |
| string ('') | string |
| String (new String()) | |
| boolean (true) | boolean |
| Boolean (new Boolean()) | |
| number (true) | number |
| Number (new Number()) | |
| Date | date |
| Symbol | symbol |
| undefined | undefined |
| null | null |
| function | function |
| Object ({}) | object |
| Object (new Object()) | |
| other | unknown |
| Initial "type" | Coerced type | Example |
|---|---|---|
| boolean | string | true -> 1 |
| number | string | '1' -> 1 |
| string | string | 'a' -> a |
| null | string (empty) | null -> |
| undefined | string (empty) | undefined -> |
See changelog For v2 changes see changelog-v2
Full API docs could be found in docs.
require('node-object-hash').hasher([options]);Returns preconfigured object with API
Parameters:
Returns hash string.
Returns sorted string generated from object (can be used for object comparison)
In order to serialize and hash your custom objects you may provide .toHashableString() method for your object. It should return string that will be hashed. You may use objectSorter and pass notable fields to it in your .toHashableString method.
For typescript users you may add to your classes implements Hashable.
var { hasher } = require('node-object-hash');
var hashSortCoerce = hasher({ sort: true, coerce: true });
// or
// var hashSortCoerce = hasher();
// or
// var hashSort = hasher({sort:true, coerce:false});
// or
// var hashCoerce = hasher({sort:false, coerce:true});
var objects = {
a: {
a: [{ c: 2, a: 1, b: { a: 3, c: 2, b: 0 } }],
b: [1, 'a', {}, null],
},
b: {
b: ['a', 1, {}, undefined],
a: [{ c: '2', b: { b: false, c: 2, a: '3' }, a: true }],
},
c: ['4', true, 0, 2, 3],
};
hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b);
// returns true
hashSortCoerce.sort(object.c);
// returns '[0,1,2,3,4]'For more examples you can see tests or try it out online at runkit
Bench data - array of 100000 complex objects
| Hashing algorithm | Result hash bytes length | Performance (ops/sec) |
|---|---|---|
| sha256 (default) | 64 | 1,599 +- 5.77% |
| sha1 | 40 | 1,983 +- 1.50% |
| sha224 | 56 | 1,701 +- 2.81% |
| sha384 | 96 | 1,800 +- 0.81% |
| sha512 | 128 | 1,847 +- 1.75% |
| md4 | 32 | 1,971 +- 0.98% |
| md5 | 32 | 1,691 +- 3.18% |
| whirlpool | 128 | 1,487 +- 2.33% |
| Library | Time (ms) | Memory (Mb) |
|---|---|---|
| node-object-hash-0.2.1 | 5813.575 | 34 |
| node-object-hash-1.0.X | 2805.581 | 27 |
| node-object-hash-1.1.X (node v7) | 2555.583 | 27 |
| node-object-hash-1.2.X (node v7) | 2390.752 | 28 |
| node-object-hash-2.X.X (node v12) | 1990.622 | 24 |
| object-hash-1.1.5 (node v7) | 28115.553 | 39 |
| object-hash-1.1.4 | 534528.254 | 41 |
| object-hash-1.1.3 | ERROR | Out of heap memory |
| hash-object-0.1.7 | 9219.826 | 42 |
| Library (node v12) | Perf (ops/s) |
|---|---|
| node-object-hash-2.0.0 | 2087 ±0.59% |
| object-hash-1.3.1 | 239 ±0.39% |
| hash-object-0.1.7 | 711 ±0.18% |
MIT
| Back | FazBrowse Home | New Git URL |