What is node-object-hash?
The node-object-hash package is a utility for creating consistent hash values from JavaScript objects. It is useful for tasks such as caching, object comparison, and ensuring data integrity.
What are node-object-hash's main functionalities?
Hashing Objects
This feature allows you to create a hash from a JavaScript object. The hash can be used to uniquely identify the object.
const objectHash = require('node-object-hash');
const hasher = objectHash();
const obj = { name: 'John', age: 30 };
const hash = hasher.hash(obj);
console.log(hash);
Custom Hashing Options
This feature allows you to customize the hashing process by specifying options such as coercion and the hashing algorithm.
const objectHash = require('node-object-hash');
const hasher = objectHash({ coerce: true, alg: 'sha256' });
const obj = { name: 'John', age: 30 };
const hash = hasher.hash(obj);
console.log(hash);
Hashing Arrays
This feature allows you to create a hash from an array, ensuring that the array's contents are uniquely identified.
const objectHash = require('node-object-hash');
const hasher = objectHash();
const arr = [1, 2, 3, 4];
const hash = hasher.hash(arr);
console.log(hash);
Other packages similar to node-object-hash
object-hash
The object-hash package is another utility for creating hash values from JavaScript objects. It offers similar functionality to node-object-hash but with a different API and additional features such as support for circular references.
hash-it
The hash-it package provides a simple and fast way to hash JavaScript objects. It is designed to be lightweight and easy to use, making it a good alternative to node-object-hash for basic hashing needs.
crypto
The built-in crypto module in Node.js can also be used to create hash values from objects. While it requires more manual setup compared to node-object-hash, it offers greater flexibility and is part of the standard library.
node-object-hash
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.
Hashes are built on top of node's crypto module
(so for using in browser use something like browserify or use crypto functions polyfills).
Installation
npm i node-object-hash
Features
- Supports object property sorting for constant hashes for objects with same properties, but different order.
- Supports ES6 (Weak)Maps and (Weak)Sets.
- Supports type coercion (e.g. 1 and "1" will be the same)
- rules:
- numbers and strings represented without quotes;
- boolean values converted to numbers;
- Supports all hashes and encodings of crypto library
- Supports large objects and arrays
Changes
v0.x.x -> v1.0.0
- Sorting mechanism rewritten form ES6 Maps to simple arrays
(add <=node-4.0.0 support)
- Performance optimization (~2 times faster than 0.x.x)
- API changes:
- Now module returns 'constructor' function, where you can set
default parameters:
var objectHash = require('node-object-hash')(options);
In case if you still need an old 0.x.x version it's available in hash.js
file.
API
Constructor require('node-object-hash')([options])
Returns preconfigured object with API
Parameters:
options
:<object>
- object with hasher config optionsoptions.coerce
:<boolean>
- if true performs type coercion (default: true
);
e.g. hash(true) == hash('1') == hash(1)
, hash(false) == hash('0') == hash(0)
options.sort
:<boolean>
- if true performs sorting on objects, arrays, etc. (default: true
);options.alg
:<string>
- sets default hash algorithm (default: 'sha256'
); can be overridden in hash
method;options.enc
:<string>
- sets default hash encoding (default: 'hex'
); can be overridden in hash
method;
API methods
hash(object[, options])
Returns hash string.
object
:<*>
object for calculating hash;options
:<object>
object with options;options.alg
:<string>
- hash algorithm (default: 'sha256'
);options.enc
:<string>
- hash encoding (default: 'hex'
);
sortObject(object)
Returns sorted string generated from object (can be used for object comparison)
object
:<*>
- object for sorting;
Requirements
version >=1.0.0
version >=0.1.0 && <1.0.0
>=nodejs-6.0.0
>=nodejs-4.0.0
(requires to run node with --harmony
flag)
browsers
- nodejs
crypto
module (for browsers)
Example
var hasher = require('node-object-hash');
var hashSortCoerce = hasher({sort:true, 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);
hashSortCoerce.sortObject(object.c)
For more examples you can see tests file
Benchmark results
Bench data - array of 100000 complex objects
Usage
npm run bench
Results
Library | Time (ms) | Memory (Mb) |
---|
node-object-hash-0.2.1 | 5813.575 | 34 |
node-object-hash-1.0.0 | 2805.581 | 27 |
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 |
Similar libraries
License
ISC