Hashing strings2 (v40)

Revision 40 of this benchmark created by kg on


Description

Looking for the fastest hash function to get a unique id of a string.

Setup

function bitwise(str){
        var hash = 0;
        if (str.length == 0) return hash;
        for (var i = 0; i < str.length; i++) {
            var char = str.charCodeAt(i);
            hash = ((hash<<5)-hash)+char;
        }
        return hash;
    }
    
    function bitwiseconv(str){
        var hash = 0;
        if (str.length == 0) return hash;
        for (var i = 0; i < str.length; i++) {
            var char = str.charCodeAt(i);
            hash = ((hash<<5)-hash)+char;
            hash = hash & hash; // Convert to 32bit integer
        }
        return hash;
    }
    
    function test1(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
      }
      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)
bitwiseconv('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Bitwise based function
test1('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
ready
Number based function
test1('[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.