Skip to content

Instantly share code, notes, and snippets.

@jessearmand
Created December 17, 2014 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessearmand/7710a1f2e79b313b5521 to your computer and use it in GitHub Desktop.
Save jessearmand/7710a1f2e79b313b5521 to your computer and use it in GitHub Desktop.
string-hash and normal hash
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