1 | # node-object-hash
|
2 |
|
3 | Tiny and fast node.js object hash library with properties/arrays sorting to provide constant hashes.
|
4 | It also provides a method that returns sorted object strings that can be used for object comparison without hashes.
|
5 | One of the fastest among other analogues (see [benchmarks](#Benchmarks)).
|
6 |
|
7 | Hashes are built on top of node's crypto module
|
8 | (so for using in browser use something like [browserify-crypto](https://github.com/crypto-browserify/crypto-browserify) or some kind of crypto functions polyfills). Or you can use only `objectSorter` ([source](https://github.com/SkeLLLa/node-object-hash/blob/master/objectSorter.js)) for getting your objects' string representation and compare or pass them to your own hash function.
|
9 |
|
10 | [![node](https://img.shields.io/node/v/node-object-hash.svg?maxAge=21600&style=flat-square)]()
|
11 | [![NPM](https://img.shields.io/npm/v/node-object-hash.svg?maxAge=21600&style=flat-square)](https://npmjs.org/packages/node-object-hash)
|
12 | [![NPM Downloads](https://img.shields.io/npm/dt/node-object-hash.svg?maxAge=21600&style=flat-square)](https://npmjs.org/packages/node-object-hash)
|
13 | [![Build Status](https://img.shields.io/travis/SkeLLLa/node-object-hash.svg?maxAge=21600&branch=master&style=flat-square)](https://travis-ci.org/SkeLLLa/node-object-hash)
|
14 | [![Known Vulnerabilities](https://snyk.io/test/github/SkeLLLa/node-object-hash/badge.svg?maxAge=21600&style=flat-square)](https://snyk.io/test/github/skellla/node-object-hash)
|
15 | [![Code Climate](https://img.shields.io/codeclimate/github/SkeLLLa/node-object-hash.svg?maxAge=21600&style=flat-square)](https://codeclimate.com/github/SkeLLLa/node-object-hash/code)
|
16 | [![Coverage](https://img.shields.io/codeclimate/coverage/github/SkeLLLa/node-object-hash.svg?maxAge=21600&style=flat-square)](https://codeclimate.com/github/SkeLLLa/node-object-hash/coverage)
|
17 | [![Analytics](https://ga-beacon.appspot.com/UA-90571586-1/node-object-hash/readme?pixel&useReferer)](https://github.com/igrigorik/ga-beacon)
|
18 |
|
19 | # Installation
|
20 |
|
21 | `npm i node-object-hash -S`
|
22 |
|
23 | # Features
|
24 | - Supports object property sorting for constant hashes for objects with same properties, but different order.
|
25 | - Supports ES6 (Weak)Maps and (Weak)Sets.
|
26 | - Supports type coercion (e.g. 1 and "1" will be the same)
|
27 | - rules:
|
28 | - numbers and strings represented without quotes;
|
29 | - boolean values converted to numbers;
|
30 | - Supports all hashes and encodings of crypto library
|
31 | - Supports large objects and arrays
|
32 | - Very fast comparing to other libs (see [Benchmarks](#Benchmarks) section)
|
33 |
|
34 | # Changes
|
35 |
|
36 | ## v0.x.x -> v1.0.0
|
37 |
|
38 | - Sorting mechanism rewritten form ES6 Maps to simple arrays
|
39 | (add <=node-4.0.0 support)
|
40 | - Performance optimization (~2 times faster than 0.x.x)
|
41 | - API changes:
|
42 | - Now module returns 'constructor' function, where you can set
|
43 | default parameters: ```var objectHash = require('node-object-hash')(options);```
|
44 |
|
45 | In case if you still need an old 0.x.x version it's available in `hash.js`
|
46 | file.
|
47 |
|
48 | ## v1.0.X -> v1.1.X
|
49 |
|
50 | Mainly all changes affected codestyle and documentation to provide better
|
51 | experience using this library. There are no changes that should affect
|
52 | functionality.
|
53 |
|
54 | - Renamed `sortObject` function to `sort` (old one is still present in code
|
55 | for backward compatibility).
|
56 | - Performed some refactoring for better codestyle and documentation.
|
57 | - Old version (`0.X.X`) moved to subfolder (`./v0`).
|
58 | - Advanced API reference added: [link](#Full API docs).
|
59 |
|
60 | # API overview
|
61 |
|
62 | ## Constructor `require('node-object-hash')([options])`
|
63 |
|
64 | Returns preconfigured object with API
|
65 |
|
66 | Parameters:
|
67 | * `options`:`object` - object with hasher config options
|
68 | * `options.coerce`:`boolean` - if true performs type coercion (default: `true`);
|
69 | e.g. `hash(true) == hash('1') == hash(1)`, `hash(false) == hash('0') == hash(0)`
|
70 | * `options.sort`:`boolean` - if true performs sorting on objects, arrays, etc. (default: `true`);
|
71 | * `options.alg`:`string` - sets default hash algorithm (default: `'sha256'`); can be overridden in `hash` method;
|
72 | * `options.enc`:`string` - sets default hash encoding (default: `'hex'`); can be overridden in `hash` method;
|
73 |
|
74 | ## API methods
|
75 |
|
76 | ### `hash(object[, options])`
|
77 |
|
78 | Returns hash string.
|
79 | * `object`:`*` object for calculating hash;
|
80 | * `options`:`object` object with options;
|
81 | * `options.alg`:`string` - hash algorithm (default: `'sha256'`);
|
82 | * `options.enc`:`string` - hash encoding (default: `'hex'`);
|
83 |
|
84 | ### `sort(object)`
|
85 |
|
86 | Returns sorted string generated from object (can be used for object comparison)
|
87 | * `object`:`*` - object for sorting;
|
88 |
|
89 | # Full API docs
|
90 |
|
91 | {{>main}}
|
92 |
|
93 | # Requirements
|
94 |
|
95 | ## version \>=1.0.0
|
96 | - `>=nodejs-0.10.0`
|
97 |
|
98 | ## version \>=0.1.0 && <1.0.0
|
99 | - `>=nodejs-6.0.0`
|
100 | - `>=nodejs-4.0.0` (requires to run node with `--harmony` flag)
|
101 |
|
102 | ## browsers
|
103 | - nodejs `crypto` module for browsers (e.g. [browserify-crypto](https://github.com/crypto-browserify/crypto-browserify)).
|
104 |
|
105 | # Example
|
106 | ```js
|
107 | var hasher = require('node-object-hash');
|
108 |
|
109 | var hashSortCoerce = hasher({sort:true, coerce:true});
|
110 | // or
|
111 | // var hashSortCoerce = hasher();
|
112 | // or
|
113 | // var hashSort = hasher({sort:true, coerce:false});
|
114 | // or
|
115 | // var hashCoerce = hasher({sort:false, coerce:true});
|
116 |
|
117 | var objects = {
|
118 | a: {
|
119 | a: [{c: 2, a: 1, b: {a: 3, c: 2, b: 0}}],
|
120 | b: [1, 'a', {}, null],
|
121 | },
|
122 | b: {
|
123 | b: ['a', 1, {}, undefined],
|
124 | a: [{c: '2', b: {b: false, c: 2, a: '3'}, a: true}]
|
125 | },
|
126 | c: ['4', true, 0, 2, 3]
|
127 | };
|
128 |
|
129 | hashSortCoerce.hash(objects.a) === hashSortCoerce.hash(objects.b);
|
130 | // returns true
|
131 |
|
132 | hashSortCoerce.sort(object.c);
|
133 | // returns '[0,1,2,3,4]'
|
134 | ```
|
135 |
|
136 | For more examples you can see [tests file](https://github.com/SkeLLLa/node-object-hash/blob/master/test/hash2.js)
|
137 | or try it out online at [runkit](https://runkit.com/skellla/node-object-hash-example)
|
138 |
|
139 | # Benchmarks
|
140 |
|
141 | Bench data - array of 100000 complex objects
|
142 |
|
143 | ## Usage
|
144 |
|
145 | * `npm run bench` to run custom benchmark
|
146 | * `npm run bench2` to run benchmark suite
|
147 |
|
148 | ## Results
|
149 |
|
150 | ### Custom benchmark ([code](bench/index.js))
|
151 |
|
152 | | Library | Time (ms) | Memory (Mb) |
|
153 | |---------------------------------------|------------|--------------------|
|
154 | | node-object-hash-0.2.1 | 5813.575 | 34 |
|
155 | | node-object-hash-1.0.X | 2805.581 | 27 |
|
156 | | node-object-hash-1.1.X (node v7) | 2555.583 | 27 |
|
157 | | object-hash-1.1.5 (node v7) | 28115.553 | 39 |
|
158 | | object-hash-1.1.4 | 534528.254 | 41 |
|
159 | | object-hash-1.1.3 | ERROR | Out of heap memory |
|
160 | | hash-object-0.1.7 | 9219.826 | 42 |
|
161 |
|
162 | ### Benchmark suite module ([code](bench/bench.js))
|
163 |
|
164 | ```
|
165 | node-object-hash x 844 ops/sec ±2.51% (82 runs sampled)
|
166 | node-object-hash-v0 x 540 ops/sec ±1.34% (82 runs sampled)
|
167 | hash-object x 310 ops/sec ±0.88% (81 runs sampled)
|
168 | object-hash x 107 ops/sec ±1.66% (72 runs sampled)
|
169 | ```
|
170 |
|
171 | ## Links
|
172 |
|
173 | * https://www.npmjs.com/package/object-hash (Slow, useful for browsers because it not uses node's crypto library)
|
174 | * https://www.npmjs.com/package/hash-object (no ES6 types support)
|
175 |
|
176 | # License
|
177 |
|
178 | ISC
|
179 |
|
\ | No newline at end of file |