Script Preparation code:
x
 
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;
}
function hashCode4(str) {
  var hash = 0, i = 0, length = str.length;
  for (; i < length; i++) {
    hash += str.charCodeAt(i) * (31**(length - i - 1));
    hash |= 0;
  }
  return hash;
}
Tests:
  • hashCode1

     
    hashCode1('qwerty')
  • hashCode2

     
    hashCode2('qwerty')
  • hashCode3

     
    hashCode3('qwerty')
  • hashCode4

     
    hashCode4('qwerty')
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    hashCode1
    hashCode2
    hashCode3
    hashCode4

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 4 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
Chrome 81 on Windows
View result in a separate tab
Test name Executions per second
hashCode1 552478.7 Ops/sec
hashCode2 12984624.0 Ops/sec
hashCode3 12713392.0 Ops/sec
hashCode4 4373711.5 Ops/sec