Hashing strings

JavaScript performance comparison

Revision 22 of this test case created by svub

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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 & hash; // 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 & hash;
      }
      return hash;
  }
  
  function bitwiseReduce(str){
      return str.split("").reduce(function(a,b)
          {a=((a<<5)-a)+b.charCodeAt(0);return a&a}
      ,0);              
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/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');
pending…
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');
pending…
Bitwise based function
bitwiseconv('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
pending…
Number based function
numbersconv('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application');
pending…
Reduce + bitwise
bitwiseReduce('[LOG] This is a a not-too-long log message, once that will commonly pop up in my application')
pending…

Compare results of other browsers

Revisions

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

0 Comments