rg443

javascript string hashing [benchmark]

Jan 26th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   Benchmark.prototype.setup = function() {
  2.     function bitwise(str){
  3.         var hash = 0;
  4.         if (str.length == 0) return hash;
  5.         for (i = 0; i < str.length; i++) {
  6.             char = str.charCodeAt(i);
  7.             hash = ((hash<<5)-hash)+char;
  8.         }
  9.         return hash;
  10.     }
  11.    
  12.     function numbers(str) {
  13.        var res = 0,
  14.            len = str.length;
  15.        for (var i = 0; i < len; i++) {
  16.         res = res * 31 + str.charCodeAt(i);
  17.        }
  18.        return res;
  19.       }
  20.    
  21.     function bitwiseconv(str){
  22.         var hash = 0;
  23.         if (str.length == 0) return hash;
  24.         for (i = 0; i < str.length; i++) {
  25.             char = str.charCodeAt(i);
  26.             hash = ((hash<<5)-hash)+char;
  27.             hash = hash & hash; // Convert to 32bit integer
  28.         }
  29.         return hash;
  30.     }
  31.    
  32.     function numbersconv(str) {
  33.        var res = 0,
  34.            len = str.length;
  35.        for (var i = 0; i < len; i++) {
  36.         res = res * 31 + str.charCodeAt(i);
  37.         res = res & res;
  38.        }
  39.        return res;
  40.       }
  41.    
  42.     function murmurhash3_32_gc(key, seed) {
  43.         var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
  44.    
  45.         remainder = key.length & 3; // key.length % 4
  46.         bytes = key.length - remainder;
  47.         h1 = seed;
  48.         c1 = 0xcc9e2d51;
  49.         c2 = 0x1b873593;
  50.         i = 0;
  51.    
  52.         while (i < bytes) {
  53.                 k1 =
  54.                   ((key.charCodeAt(i) & 0xff)) |
  55.                   ((key.charCodeAt(++i) & 0xff) << 8) |
  56.                   ((key.charCodeAt(++i) & 0xff) << 16) |
  57.                   ((key.charCodeAt(++i) & 0xff) << 24);
  58.                 ++i;
  59.    
  60.                 k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
  61.                 k1 = (k1 << 15) | (k1 >>> 17);
  62.                 k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
  63.    
  64.                 h1 ^= k1;
  65.             h1 = (h1 << 13) | (h1 >>> 19);
  66.                 h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
  67.                 h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
  68.         }
  69.    
  70.         k1 = 0;
  71.    
  72.         switch (remainder) {
  73.                 case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
  74.                 case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
  75.                 case 1: k1 ^= (key.charCodeAt(i) & 0xff);
  76.    
  77.                 k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
  78.                 k1 = (k1 << 15) | (k1 >>> 17);
  79.                 k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
  80.                 h1 ^= k1;
  81.         }
  82.    
  83.         h1 ^= key.length;
  84.    
  85.         h1 ^= h1 >>> 16;
  86.         h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
  87.         h1 ^= h1 >>> 13;
  88.         h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
  89.         h1 ^= h1 >>> 16;
  90.    
  91.         return h1 >>> 0;
  92.     }
  93.    
  94.   };
Advertisement
Add Comment
Please, Sign In to add comment