$hashCode$String$x

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function go() {
      $hashCode$String('');
      $hashCode$String('Hello');
      $hashCode$String('a man a plan a canal panama');
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
b29
function $hashCode$String(s) {
    var hash = 0, len = s.length, mask = ((1 << 29) - 1);
    for (var i = 0; i < len; i++) {
      var ch = s.charCodeAt(i);
      hash = (hash + ch) & mask;
      hash = (hash + ((hash & (mask >> 10)) << 10)) & mask;
      hash = (hash ^ (hash >> 6));
    }

    hash = (hash + ((hash & (mask >> 3)) << 3)) & mask;
    hash = hash ^ (hash >> 11);
    hash = (hash + ((hash & (mask >> 15)) << 15)) & mask;

    return hash;
}
go()
pending…
b31
function $hashCode$String(s) {
    var hash = 0, len = s.length, mask = ((1 << 31) - 1);
    for (var i = 0; i < len; i++) {
      var ch = s.charCodeAt(i);
      hash = (hash + ch) & mask;
      hash = (hash + ((hash & (mask >> 10)) << 10)) & mask;
      hash = (hash ^ (hash >> 6));
    }

    hash = (hash + ((hash & (mask >> 3)) << 3)) & mask;
    hash = hash ^ (hash >> 11);
    hash = (hash + ((hash & (mask >> 15)) << 15)) & mask;

    return hash;
}
go()
pending…
naive
function $hashCode$String(s) {
    var hash = 0, len = s.length, mask = ((1 << 29) - 1);
    for (var i = 0; i < len; i++) {
      var ch = s.charCodeAt(i);
      hash += ch;
      hash += hash << 10;
      hash ^= hash >> 6;
    }

    hash +=  hash << 3;
    hash = hash ^ (hash >> 11);
    hash += hash << 15;

    return hash & mask;
}
go()
pending…
b30
function $hashCode$String(s) {
    var hash = 0, len = s.length, mask = ((1 << 30) - 1);
    for (var i = 0; i < len; i++) {
      var ch = s.charCodeAt(i);
      hash = (hash + ch) & mask;
      hash = (hash + ((hash & (mask >> 10)) << 10)) & mask;
      hash = (hash ^ (hash >> 6));
    }

    hash = (hash + ((hash & (mask >> 3)) << 3)) & mask;
    hash = hash ^ (hash >> 11);
    hash = (hash + ((hash & (mask >> 15)) << 15)) & mask;

    return hash;
}
go()
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. Here’s a list of current revisions for this page:

0 comments

Add a comment