Hashing strings (v43)

Revision 43 of this benchmark created on


Description

Changed length checks to use === instead of == and added bitwise and number versions using |= for the 32-bit Integer conversion.

Setup

function bitwise(str) {
      var hash = 0,
        len = str.length
      if (len === 0) return hash;
      for (var i = 0; i < len; i++) {
        char = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + char;
      }
      return hash;
    }
    
    function numbers(str) {
      var hash = 0,
        len = str.length
      if (len === 0) return hash;
      for (var i = 0; i < len; i++) {
        hash = hash * 31 + str.charCodeAt(i);
      }
      return hash;
    }
    
    function bitwiseconv(str) {
      var hash = 0,
        len = str.length
      if (len === 0) return hash;
      for (var i = 0; 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
      if (len === 0) return hash;
      for (var i = 0; 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
      if (len === 0) return hash;
      for (var i = 0; i < len; i++) {
        hash = hash * 31 + str.charCodeAt(i);
        hash &= hash;
      }
      return hash;
    }
    
    function numbersconv2(str) {
      var hash = 0,
        len = str.length
      if (len === 0) return hash;
      for (var i = 0; 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,
        keyLength = key.length,
        i = 0;
    
      for (; keyLength >= 8; keyLength -= 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 (keyLength) {
        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;
    }
    
    function splitreduce(s) {
      return s.split("").reduce(function(a, b) {
        a = ((a << 5) - a) + b.charCodeAt(0);
        return a & a
      }, 0);
    }

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
Split & Reduce
splitreduce('[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.