Hashing strings (v47)

Revision 47 of this benchmark created on


Description

Changed length checks to !len, added missing returns for empty strings, normalized vars.

Setup

function fnv32a(str) {
        var hash = 0x811c9dc5, len = str.length, i = 0;
        if(!len) return hash;
        for (; i < len; ++i) {
            hash ^= str.charCodeAt(i);
            hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
        }
        return hash >>> 0;
    }
    
    function bitwise(str){
        var hash = 0, len = str.length, i = 0;
        if (!len) return hash;
        for (; i < len; ++i) {
            char = str.charCodeAt(i);
            hash = ((hash << 5) - hash) + char;
        }
        return hash;
    }
    
    function numbers(str) {
        var hash = 0, len = str.length, i = 0;
        if (!len) return hash;
        for (; i < len; ++i) {
            hash = hash * 31 + str.charCodeAt(i);
        }
        return hash;
    }
    
    function bitwiseconv(str){
        var hash = 0, len = str.length, i = 0;
        if (!len) return hash;
        for (; i < len; ++i) {
            hash = ((hash<<5)-hash)+str.charCodeAt(i);
            hash &= hash; // Convert to 32bit integer
        }
        return hash;
    }
    
    function bitwiseconv2(str){
        var hash = 0, len = str.length, i = 0;
        if (!len) return hash;
        for (; i < len; ++i) {
            hash = ((hash<<5)-hash)+str.charCodeAt(i);
            hash |= 0; // Convert to 32bit integer
        }
        return hash;
    }
    
    function numbersconv(str) {
        var hash = 0, len = str.length, i = 0;
        if (!len) return hash;
        for (; i < len; i++) {
            hash = hash * 31 + str.charCodeAt(i);
            hash &= hash;
        }
        return hash;
    }
    
    function numbersconv2(str) {
        var hash = 0, len = str.length, i = 0;
        if (!len) return hash;
        for (; i < len; i++) {
            hash = hash * 31 + str.charCodeAt(i);
            hash |= 0;
        }
        return hash;
    }
    
    function bitwiseReduce(str){
        var length = str.length,
            a = 0, i = 0;
        for (; i < length; ++i) {
          a = ((a << 5) - a) + str.charCodeAt(i);
          a = a & a;
        }
        return a;      
    }
    
    function djbhash (key) {
      var hash = 5381,
          len = key.length,
          i = 0;
      if(!len) return hash;
      for (; len >= 8; len -= 8) {
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      }
      switch (len) {
      case 7: hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      case 6: hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      case 5: hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      case 4: hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      case 3: hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      case 2: hash = ((hash << 5) + hash) + key.charCodeAt(i++);
      case 1:
        hash = ((hash << 5) + hash) + key.charCodeAt(i++);
        break;
      case 0:
        break;
      }
      return hash;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Bitwise based function (No integer conversion)
bitwise('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Number based function (No integer conversion)
numbers('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Bitwise based function
bitwiseconv('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Number based function
numbersconv('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Reduce + bitwise
bitwiseReduce('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application')
ready
DJB
djbhash('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application')
ready
Bitwise based function (Integer convert with |=)
bitwiseconv2('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Number based function (Integer convert with |=)
numbersconv2('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
JSON stringify
JSON.stringify('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
FNV 32bit
fnv32a('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.