Gathering detailed insights and metrics for node-object-hash
Gathering detailed insights and metrics for node-object-hash
Gathering detailed insights and metrics for node-object-hash
Gathering detailed insights and metrics for node-object-hash
object-hash
Generate hashes from javascript objects in node and the browser.
node-red-contrib-object-hash
A Node-RED node for generate hashes from objects and values in node.
object-hash-import
Generate hashes from javascript objects in node.
create-object-hash
Generate hashes from javascript objects in node and the browser.
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.
npm install node-object-hash
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.2
Supply Chain
100
Quality
77.9
Maintenance
100
Vulnerability
100
License
TypeScript (91.66%)
JavaScript (8.34%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
88 Stars
214 Commits
20 Forks
3 Watchers
3 Branches
11 Contributors
Updated on Jun 10, 2025
Latest Version
3.1.1
Package Id
node-object-hash@3.1.1
Unpacked Size
71.31 kB
Size
15.04 kB
File Count
19
NPM Version
10.9.0
Node Version
22.12.0
Published on
Dec 17, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
38
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.
coerce=false
Set
s will no longer generate the same hashes as Array
s. In order to restore previous behavior set coerce.set=true
.coerce=false
Symbol
s will generate hash based on symbol .toString
value. That's useful for Symbol.for('smth')
. If coerce.symbol=true
all Symbols
s will have equal hashes.
TLDR; If you use library with Set
s or Symbol
s with coerce=false
in order to keep hashes the same as in v1.X.X
you should use following constructor:const hasher = require('node-object-hash')({coerce: {set: true, symbol: true}})
dist
directory. If you required it directly via require('node-object-hash/objectSorter')
you should change it to require('node-object-hash/dist/objectSorter').v0
version from code.trim
option. It can be used to remove unncecessary spaces in string
s or function
bodies.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.
1require('node-object-hash').hasher([options]);
Returns preconfigured object with API
Parameters:
options
:object
- object with hasher config optionsoptions.coerce
:boolean|object
- if true performs type coercion (default: true
);
e.g. hash(true) == hash('1') == hash(1)
, hash(false) == hash('0') == hash(0)
options.sort
:boolean|object
- if true performs sorting on objects, arrays, etc. (default: true
); in order to
perform sorting on TypedArray
(Buffer
, Int8Array
, etc.), specify it explicitly: typedArray: true
options.trim
:boolean|object
- if true performs trim of spaces and replaces space-like characters with single space (default: false
);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;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'
);sort(object)
Returns sorted string generated from object (can be used for object comparison)
object
:*
- object for sorting;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
.
>=nodejs-0.10.0
>=nodejs-6.0.0
>=nodejs-4.0.0
(requires to run node with --harmony
flag)1var { hasher } = require('node-object-hash'); 2 3var hashSortCoerce = hasher({ sort: true, coerce: true }); 4// or 5// var hashSortCoerce = hasher(); 6// or 7// var hashSort = hasher({sort:true, coerce:false}); 8// or 9// var hashCoerce = hasher({sort:false, coerce:true}); 10 11var objects = { 12 a: { 13 a: [{ c: 2, a: 1, b: { a: 3, c: 2, b: 0 } }], 14 b: [1, 'a', {}, null], 15 }, 16 b: { 17 b: ['a', 1, {}, undefined], 18 a: [{ c: '2', b: { b: false, c: 2, a: '3' }, a: true }], 19 }, 20 c: ['4', true, 0, 2, 3], 21}; 22 23hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b); 24// returns true 25 26hashSortCoerce.sort(object.c); 27// 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
npm run bench
to run custom benchmarknpm run benchmark
to run benchmark suitenpm run benchmark:hash
to run hash benchmark suiteHashing 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
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 3/23 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More