[d565449] | 1 | iMurmurHash.js
|
---|
| 2 | ==============
|
---|
| 3 |
|
---|
| 4 | An incremental implementation of the MurmurHash3 (32-bit) hashing algorithm for JavaScript based on [Gary Court's implementation](https://github.com/garycourt/murmurhash-js) with [kazuyukitanimura's modifications](https://github.com/kazuyukitanimura/murmurhash-js).
|
---|
| 5 |
|
---|
| 6 | This version works significantly faster than the non-incremental version if you need to hash many small strings into a single hash, since string concatenation (to build the single string to pass the non-incremental version) is fairly costly. In one case tested, using the incremental version was about 50% faster than concatenating 5-10 strings and then hashing.
|
---|
| 7 |
|
---|
| 8 | Installation
|
---|
| 9 | ------------
|
---|
| 10 |
|
---|
| 11 | To use iMurmurHash in the browser, [download the latest version](https://raw.github.com/jensyt/imurmurhash-js/master/imurmurhash.min.js) and include it as a script on your site.
|
---|
| 12 |
|
---|
| 13 | ```html
|
---|
| 14 | <script type="text/javascript" src="/scripts/imurmurhash.min.js"></script>
|
---|
| 15 | <script>
|
---|
| 16 | // Your code here, access iMurmurHash using the global object MurmurHash3
|
---|
| 17 | </script>
|
---|
| 18 | ```
|
---|
| 19 |
|
---|
| 20 | ---
|
---|
| 21 |
|
---|
| 22 | To use iMurmurHash in Node.js, install the module using NPM:
|
---|
| 23 |
|
---|
| 24 | ```bash
|
---|
| 25 | npm install imurmurhash
|
---|
| 26 | ```
|
---|
| 27 |
|
---|
| 28 | Then simply include it in your scripts:
|
---|
| 29 |
|
---|
| 30 | ```javascript
|
---|
| 31 | MurmurHash3 = require('imurmurhash');
|
---|
| 32 | ```
|
---|
| 33 |
|
---|
| 34 | Quick Example
|
---|
| 35 | -------------
|
---|
| 36 |
|
---|
| 37 | ```javascript
|
---|
| 38 | // Create the initial hash
|
---|
| 39 | var hashState = MurmurHash3('string');
|
---|
| 40 |
|
---|
| 41 | // Incrementally add text
|
---|
| 42 | hashState.hash('more strings');
|
---|
| 43 | hashState.hash('even more strings');
|
---|
| 44 |
|
---|
| 45 | // All calls can be chained if desired
|
---|
| 46 | hashState.hash('and').hash('some').hash('more');
|
---|
| 47 |
|
---|
| 48 | // Get a result
|
---|
| 49 | hashState.result();
|
---|
| 50 | // returns 0xe4ccfe6b
|
---|
| 51 | ```
|
---|
| 52 |
|
---|
| 53 | Functions
|
---|
| 54 | ---------
|
---|
| 55 |
|
---|
| 56 | ### MurmurHash3 ([string], [seed])
|
---|
| 57 | Get a hash state object, optionally initialized with the given _string_ and _seed_. _Seed_ must be a positive integer if provided. Calling this function without the `new` keyword will return a cached state object that has been reset. This is safe to use as long as the object is only used from a single thread and no other hashes are created while operating on this one. If this constraint cannot be met, you can use `new` to create a new state object. For example:
|
---|
| 58 |
|
---|
| 59 | ```javascript
|
---|
| 60 | // Use the cached object, calling the function again will return the same
|
---|
| 61 | // object (but reset, so the current state would be lost)
|
---|
| 62 | hashState = MurmurHash3();
|
---|
| 63 | ...
|
---|
| 64 |
|
---|
| 65 | // Create a new object that can be safely used however you wish. Calling the
|
---|
| 66 | // function again will simply return a new state object, and no state loss
|
---|
| 67 | // will occur, at the cost of creating more objects.
|
---|
| 68 | hashState = new MurmurHash3();
|
---|
| 69 | ```
|
---|
| 70 |
|
---|
| 71 | Both methods can be mixed however you like if you have different use cases.
|
---|
| 72 |
|
---|
| 73 | ---
|
---|
| 74 |
|
---|
| 75 | ### MurmurHash3.prototype.hash (string)
|
---|
| 76 | Incrementally add _string_ to the hash. This can be called as many times as you want for the hash state object, including after a call to `result()`. Returns `this` so calls can be chained.
|
---|
| 77 |
|
---|
| 78 | ---
|
---|
| 79 |
|
---|
| 80 | ### MurmurHash3.prototype.result ()
|
---|
| 81 | Get the result of the hash as a 32-bit positive integer. This performs the tail and finalizer portions of the algorithm, but does not store the result in the state object. This means that it is perfectly safe to get results and then continue adding strings via `hash`.
|
---|
| 82 |
|
---|
| 83 | ```javascript
|
---|
| 84 | // Do the whole string at once
|
---|
| 85 | MurmurHash3('this is a test string').result();
|
---|
| 86 | // 0x70529328
|
---|
| 87 |
|
---|
| 88 | // Do part of the string, get a result, then the other part
|
---|
| 89 | var m = MurmurHash3('this is a');
|
---|
| 90 | m.result();
|
---|
| 91 | // 0xbfc4f834
|
---|
| 92 | m.hash(' test string').result();
|
---|
| 93 | // 0x70529328 (same as above)
|
---|
| 94 | ```
|
---|
| 95 |
|
---|
| 96 | ---
|
---|
| 97 |
|
---|
| 98 | ### MurmurHash3.prototype.reset ([seed])
|
---|
| 99 | Reset the state object for reuse, optionally using the given _seed_ (defaults to 0 like the constructor). Returns `this` so calls can be chained.
|
---|
| 100 |
|
---|
| 101 | ---
|
---|
| 102 |
|
---|
| 103 | License (MIT)
|
---|
| 104 | -------------
|
---|
| 105 | Copyright (c) 2013 Gary Court, Jens Taylor
|
---|
| 106 |
|
---|
| 107 | Permission is hereby granted, free of charge, to any person obtaining a copy of
|
---|
| 108 | this software and associated documentation files (the "Software"), to deal in
|
---|
| 109 | the Software without restriction, including without limitation the rights to
|
---|
| 110 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
| 111 | the Software, and to permit persons to whom the Software is furnished to do so,
|
---|
| 112 | subject to the following conditions:
|
---|
| 113 |
|
---|
| 114 | The above copyright notice and this permission notice shall be included in all
|
---|
| 115 | copies or substantial portions of the Software.
|
---|
| 116 |
|
---|
| 117 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 118 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
---|
| 119 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
---|
| 120 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
---|
| 121 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
---|
| 122 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|