Test name | Executions per second |
---|---|
hashCode1 | 577716.1 Ops/sec |
hashCode2 | 12134375.0 Ops/sec |
hashCode3 | 12714440.0 Ops/sec |
function hashCode1(str){
var hash = 0;
if (str.length == 0) return hash;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
function hashCode2(str) {
var hash = 0, i, chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
function hashCode3(str) {
var hash = 5381,
i = str.length;
while(i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return hash >>> 0;
}
hashCode1('qwerty')
hashCode2('qwerty')
hashCode3('qwerty')