Created
December 17, 2014 04:15
-
-
Save jessearmand/7710a1f2e79b313b5521 to your computer and use it in GitHub Desktop.
string-hash and normal hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function slow_hash(str) { | |
var hash = 0, i, chr, len; | |
if (str.length === 0) return hash; | |
for (i = 0, len = str.length; i < len; i++) { | |
chr = str.charCodeAt(i); | |
hash = ((hash << 5) - hash) + chr; | |
hash |= 0; // Convert to 32bit integer | |
// max of integer in javascript is 2^53 | |
// http://stackoverflow.com/questions/307179/ | |
// plus 2^31 to prevent it negative | |
hash += Math.pow(2, 31) | |
} | |
return hash; | |
} | |
var debug = require('debug')('hash') | |
var hash = require('string-hash') | |
var string = "548ffbb54fbb9d19005807f7" | |
debug(string) | |
debug(slow_hash(string)) | |
var anotherString = "548ffbb54fbb9d19005807f7" | |
debug(anotherString) | |
debug(hash(anotherString)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment