string murmur3 vs goog.string.hashCode

JavaScript performance comparison

Test case created by Francis Avila

Info

Test murmur3 string hashing vs cljs's (which uses goog.string.hashCode).

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var e = Math.imul || function(a, b) {
      var c = a & 65535, d = b & 65535;
      return c * d + ((a >>> 16 & 65535) * d + c * (b >>> 16 & 65535) << 16 >>> 0) | 0;
    };
    function g(a) {
      a = e(a, 3432918353);
      return a = e(a << 15 | a >>> 17, 461845907);
    }
    function h(a, b) {
      a ^= b;
      return a = e(a << 13 | a >>> 19, 5) + 3864292196 | 0;
    }
    function k(a, b) {
      a ^= b;
      a = e(a ^ a >>> 16, 2246822507);
      a ^= a >>> 13;
      a = e(a, 3266489909);
      return a ^= a >>> 16;
    }
    function hashInt(a) {
      if (0 === a) {
        return 0;
      }
      a = g(a);
      a = h(0, a);
      return k(a, 4);
    };
    function hashUnencodedChars(a) {
      for (var b = 0, c = 0, d = 1, f = a.length;d < f;) {
        c = a.charCodeAt(d - 1) | a.charCodeAt(d) << 16, d += 2, c = g(c), b = h(b, c);
      }
      d === f && (c = a.charCodeAt(d - 1), c = g(c), b ^= c);
      return k(b, f + f | 0);
    };
   
    function cljs_hashInt(n) {
      return Math.floor(n) % 2147483647;
    };
   
    function goog_str_hashCode(str) {
      var result = 0;
      for (var i = 0; i < str.length; ++i) {
        result = 31 * result + str.charCodeAt(i);
        // Normalize to 4 byte range, 0 ... 2^32.
        result %= 0x100000000;
      }
      return result;
    };
   
   
    var smallstring = "test-string";
    var bigstring = "Bácòԉ ïpsûm ԁ߀l߀r sït ɑmêt ρ߀rchêttã ut íл ԁèsérùԉt fɭaлk. Läb߀ré ρòrcɦêttá fuɢíát ѕhort ɭòiԉ sɯiԉê ρãԉcêttå fâtbãck cüρiᏧãtãt seᏧ léƃerkàѕ cüɭpɑ. Irúrè ãliɋûá tùrᏧückên vêԉïsoԉ pr߀sciuttò. Irúré üt ԁeѕeruԉt p߀rchèttà còɯ c߀ϻm߀ԁ߀. Düís taïɭ pɑѕtrɑmí, pòrk ɭòïл alïqüip ést têmρòr pariåtür. Et eu cìlɭüm, ɋüiѕ moɭlìt trì-tìp ëлïϻ ƃåɭɭ tïρ seԁ ƃresàòɭa drúmstick ԁòl߀rê. Pãѕträϻi ѕɦòrt rïƃs ɋuïs Ꮷruϻstíck ɋûì ãliqúɑ tûrƙeϒ sùԉt porƙ bèlly ρr߀ïdéлt iлcìԁíԁüԉt ԉïsi cònsèɋúɑt.";
   
   
    var result = null;
    function use_result(result) {};
};

Benchmark.prototype.teardown = function() {
    result = use_result(result);
};
</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
m3 hash big string
result = hashUnencodedChars(bigstring);
pending…
cljs hash big string
result = goog_str_hashCode(bigstring);
pending…
m3 hash small string
result = hashUnencodedChars(smallstring);
pending…
cljs hash small string
result = goog_str_hashCode(smallstring);
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