numberHash.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * Threshold for switching from 32-bit to 64-bit hashing. This is selected to ensure that the bias towards lower modulo results when using 32-bit hashing is <0.5%.
  8. * @type {number}
  9. */
  10. const FNV_64_THRESHOLD = 1 << 24;
  11. /**
  12. * The FNV-1a offset basis for 32-bit hash values.
  13. * @type {number}
  14. */
  15. const FNV_OFFSET_32 = 2166136261;
  16. /**
  17. * The FNV-1a prime for 32-bit hash values.
  18. * @type {number}
  19. */
  20. const FNV_PRIME_32 = 16777619;
  21. /**
  22. * The mask for a positive 32-bit signed integer.
  23. * @type {number}
  24. */
  25. const MASK_31 = 0x7fffffff;
  26. /**
  27. * The FNV-1a offset basis for 64-bit hash values.
  28. * @type {bigint}
  29. */
  30. const FNV_OFFSET_64 = BigInt("0xCBF29CE484222325");
  31. /**
  32. * The FNV-1a prime for 64-bit hash values.
  33. * @type {bigint}
  34. */
  35. const FNV_PRIME_64 = BigInt("0x100000001B3");
  36. /**
  37. * Computes a 32-bit FNV-1a hash value for the given string.
  38. * See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
  39. * @param {string} str The input string to hash
  40. * @returns {number} - The computed hash value.
  41. */
  42. function fnv1a32(str) {
  43. let hash = FNV_OFFSET_32;
  44. for (let i = 0, len = str.length; i < len; i++) {
  45. hash ^= str.charCodeAt(i);
  46. // Use Math.imul to do c-style 32-bit multiplication and keep only the 32 least significant bits
  47. hash = Math.imul(hash, FNV_PRIME_32);
  48. }
  49. // Force the result to be positive
  50. return hash & MASK_31;
  51. }
  52. /**
  53. * Computes a 64-bit FNV-1a hash value for the given string.
  54. * See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
  55. * @param {string} str The input string to hash
  56. * @returns {bigint} - The computed hash value.
  57. */
  58. function fnv1a64(str) {
  59. let hash = FNV_OFFSET_64;
  60. for (let i = 0, len = str.length; i < len; i++) {
  61. hash ^= BigInt(str.charCodeAt(i));
  62. hash = BigInt.asUintN(64, hash * FNV_PRIME_64);
  63. }
  64. return hash;
  65. }
  66. /**
  67. * Computes a hash value for the given string and range. This hashing algorithm is a modified
  68. * version of the [FNV-1a algorithm](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function).
  69. * It is optimized for speed and does **not** generate a cryptographic hash value.
  70. *
  71. * We use `numberHash` in `lib/ids/IdHelpers.js` to generate hash values for the module identifier. The generated
  72. * hash is used as a prefix for the module id's to avoid collisions with other modules.
  73. *
  74. * @param {string} str The input string to hash.
  75. * @param {number} range The range of the hash value (0 to range-1).
  76. * @returns {number} - The computed hash value.
  77. *
  78. * @example
  79. *
  80. * ```js
  81. * const numberHash = require("webpack/lib/util/numberHash");
  82. * numberHash("hello", 1000); // 73
  83. * numberHash("hello world"); // 72
  84. * ```
  85. *
  86. */
  87. module.exports = (str, range) => {
  88. if (range < FNV_64_THRESHOLD) {
  89. return fnv1a32(str) % range;
  90. } else {
  91. return Number(fnv1a64(str) % BigInt(range));
  92. }
  93. };