Simple hashes

Benchmark created by Manuel Reil on


Setup

var strToHash = 'text:SAP FI/CO';
  
  var hashXor = function (str) {
    var hash = 5381,
        i    = str.length;
  
    while(i) {
      hash = (hash * 33) ^ str.charCodeAt(--i);
    }
  
    return hash >>> 0;
  };
  
  var djb2Code = function(str){
      var hash = 5381;
      for (i = 0; i < str.length; i++) {
          char = str.charCodeAt(i);
          hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
      }
      return hash;
  };
  
  var sdbmCode = function(str){
      var hash = 0;
      for (i = 0; i < str.length; i++) {
          char = str.charCodeAt(i);
          hash = char + (hash << 6) + (hash << 16) - hash;
      }
      return hash;
  };

Test runner

Ready to run.

Testing in
TestOps/sec
hashXor
hashXor(strToHash);
ready
djb2Code
djb2Code(strToHash);
ready
sdbmCode
sdbmCode(strToHash);
ready

Revisions

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

  • Revision 1: published by Manuel Reil on