├── .gitignore ├── .travis.yml ├── README.md ├── component.json ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | cache: 5 | directories: 6 | - node_modules 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | string-hash 2 | =========== 3 | 4 | A fast string hashing function for Node.JS. The particular algorithm is quite 5 | similar to `djb2`, by Dan Bernstein and available 6 | [here](http://www.cse.yorku.ca/~oz/hash.html). Differences include iterating 7 | over the string *backwards* (as that is faster in JavaScript) and using the XOR 8 | operator instead of the addition operator (as described at that page and 9 | because it obviates the need for modular arithmetic in JavaScript). 10 | 11 | The hashing function returns a number between 0 and 4294967295 (inclusive). 12 | 13 | Thanks to [cscott](https://github.com/cscott) for reminding us how integers 14 | work in JavaScript. 15 | 16 | Example 17 | ------- 18 | 19 | `npm install string-hash` or `yarn add string-hash`, then: 20 | 21 | ``` 22 | const stringHash = require("string-hash"); 23 | console.log(stringHash("foo")); // prints "193420387" 24 | ``` 25 | 26 | Note that the return value is always an unsigned, 32-bit integer. 27 | 28 | License 29 | ------- 30 | 31 | To the extend possible by law, The Dark Sky Company, LLC has [waived all 32 | copyright and related or neighboring rights][cc0] to this library. 33 | 34 | [cc0]: http://creativecommons.org/publicdomain/zero/1.0/ 35 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "string-hash", 3 | "repo": "darkskyapp/string-hash", 4 | "description": "fast string hashing function", 5 | "version": "1.1.1", 6 | "keywords": [ 7 | "string", 8 | "hashing" 9 | ], 10 | "dependencies": {}, 11 | "development": { 12 | "mocha": "1.3.x" 13 | }, 14 | "license": "CC0", 15 | "main": "index.js", 16 | "scripts": [ 17 | "index.js" 18 | ], 19 | "remotes": [] 20 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function hash(str) { 4 | var hash = 5381, 5 | i = str.length; 6 | 7 | while(i) { 8 | hash = (hash * 33) ^ str.charCodeAt(--i); 9 | } 10 | 11 | /* JavaScript does bitwise operations (like XOR, above) on 32-bit signed 12 | * integers. Since we want the results to be always positive, convert the 13 | * signed int to an unsigned by doing an unsigned bitshift. */ 14 | return hash >>> 0; 15 | } 16 | 17 | module.exports = hash; 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "string-hash", 3 | "version": "1.1.4", 4 | "description": "fast string hashing function", 5 | "license": "CC0-1.0", 6 | "keywords": [ 7 | "string", 8 | "hashing" 9 | ], 10 | "author": { 11 | "name": "The Dark Sky Company", 12 | "email": "developer@darksky.net" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git://github.com/darkskyapp/string-hash.git" 17 | }, 18 | "main": "./index", 19 | "dependencies": { 20 | }, 21 | "devDependencies": { 22 | "mocha": "*" 23 | }, 24 | "scripts": { 25 | "test": "mocha" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var assert = require("assert"), 3 | hash = require("./"); 4 | 5 | describe("hash", function() { 6 | it("should hash \"Mary had a little lamb.\" to 1766333550", function() { 7 | assert.equal(hash("Mary had a little lamb."), 1766333550); 8 | }); 9 | 10 | it("should hash \"Hello, world!\" to 343662184", function() { 11 | assert.equal(hash("Hello, world!"), 343662184); 12 | }); 13 | }); 14 | --------------------------------------------------------------------------------