Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-object-hash

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-object-hash - npm Package Compare versions

Comparing version 0.2.1 to 1.0.0

bench/index.js

11

package.json
{
"name": "node-object-hash",
"version": "0.2.1",
"version": "1.0.0",
"description": "Node.js object hash library with properties/arrays sorting to provide constant hashes",
"main": "index.js",
"main": "hash2.js",
"directories": {

@@ -12,6 +12,9 @@ "test": "test"

"chai": "^3.5.0",
"mocha": "^2.5.3"
"faker": "^3.1.0",
"mocha": "^2.5.3",
"object-hash": "^1.1.4"
},
"scripts": {
"test": "mocha --harmony"
"test": "mocha --harmony",
"bench": "node --expose-gc ./bench/index.js"
},

@@ -18,0 +21,0 @@ "repository": {

# node-object-hash
Node object hash library. Built on top of node's crypto module.
Node.js object hash library with properties/arrays sorting to provide constant hashes.
Built on top of node's crypto module (so for using in browser use something
like browserify or use crypto functions polyfills).
### Installation

@@ -10,3 +13,2 @@ `npm i node-object-hash`

- Supports object property sorting for constant hashes for objects with same properties, but different order.
- NOTE: object arrays should be sorted manually if needed
- Supports ES6 Maps and Sets.

@@ -20,53 +22,89 @@ - Supports type coercion (e.g. 1 and "1" will be the same)

### 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);```
### API
#### Constructor `require('node-object-hash')([options])`
Returns preconfigured object with API
Parameters:
* `options`:`<object>` - object with hasher config options
* `options.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;
#### `hash(object[, options])`
Returns hash string.
* `object` object for calculating hash;
* `options` object with options;
* `options.alg` hash algorithm (default: `'sha256'`);
* `options.enc` hash encoding (default: `'hex'`);
* `options.coerce` if true performs type coercion (default: `true`);
* `options.sort` if true performs sorting on objects, arrays and Sets (default: `true`);
* `object`:`<*>` object for calculating hash;
* `options`:`<object>` object with options;
* `options.alg`:`<string>` - hash algorithm (default: `'sha256'`);
* `options.enc`:`<string>` - hash encoding (default: `'hex'`);
#### `sortedObjectString(object[, options])`
#### `sortObject(object)`
Returns sorted string generated from object
* `object` object for calculating hash;
* `options` object with options;
* `options.coerce` if true performs type coercion (default: `true`);
* `options.sort` if true performs sorting on objects, arrays and Sets (default: `true`);
* `object`:`<*>` - object for sorting;
### Requirements
- `>=nodejs-0.10.0` (starting from version 1.0.0)
- `>=nodejs-6.0.0`
- `>=nodejs-4.0.0` (requires to run node with `--harmony` flag)
- nodejs `crypto` module
### Example
```js
const {hash} = require('node-object-hash');
var hasher = require('node-object-hash');
const object = {
x: new Map([['a', 1], ['c', 3], ['b', 2]]),
z: new Set([4,3,2,1]),
f: 2,
e: [{a:2}, 3, {g:2, e:{d:1}}, "rt", "1", "2abc", "3d"],
d: [3,5,6,1,2,3],
c: "3ab",
b: "2",
a: 1
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]
};
const hashString = hash(object);
const hashStringSha512 = hash(object, {alg:'sha512'});
hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b);
// returns true
hashSortCoerce.sortObject(object.c)
// returns '[0,1,2,3,4]'
```
### Compared to same libraries
### Benchmark results
Bench data - array of 100000 complex objects
#### Usage
`npm run bench`
#### Results
| | node-object-hash-0.2.1 | node-object-hash-1.0.0 | object-hash-1.1.4 |
|---|---|---|---|
| Time | 5773.869ms | 2961.812ms | 534528.254ms |
| Memory | ~35Mb | ~33Mb | ~41Mb |
### Similar libraries
* https://www.npmjs.com/package/object-hash
* Pros:
* node-object-hash has twice smaller memory footprint
* five times faster
* ~~no memory leak when using in sync flow~~ (fixed in last version of `object-hash`)
* Cons:
* Only `>=node-4.0.0` supported
### License
ISC
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc