fork download
  1. /**
  2. * JS Implementation of MurmurHash2
  3. *
  4. * @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
  5. * @see http://g...content-available-to-author-only...b.com/garycourt/murmurhash-js
  6. * @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a>
  7. * @see http://sites.google.com/site/murmurhash/
  8. *
  9. * @param {string} str ASCII only
  10. * @param {number} seed Positive integer only
  11. * @return {number} 32-bit positive integer hash
  12. */
  13.  
  14. function murmurhash2_32_gc(str, seed) {
  15. var
  16. l = str.length,
  17. h = seed ^ l,
  18. i = 0,
  19. k;
  20.  
  21. while (l >= 4) {
  22. k =
  23. ((str.charCodeAt(i) & 0xff)) |
  24. ((str.charCodeAt(++i) & 0xff) << 8) |
  25. ((str.charCodeAt(++i) & 0xff) << 16) |
  26. ((str.charCodeAt(++i) & 0xff) << 24);
  27.  
  28. k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
  29. k ^= k >>> 24;
  30. k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
  31.  
  32. h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16)) ^ k;
  33.  
  34. l -= 4;
  35. ++i;
  36. }
  37.  
  38. switch (l) {
  39. case 3: h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
  40. case 2: h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
  41. case 1: h ^= (str.charCodeAt(i) & 0xff);
  42. h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
  43. }
  44.  
  45. h ^= h >>> 13;
  46. h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
  47. h ^= h >>> 15;
  48.  
  49. return h >>> 0;
  50. }
  51. console.log(murmurhash2_32_gc('str', 15));
Success #stdin #stdout 0.05s 18560KB
stdin
Standard input is empty
stdout
797866228