(string: string)
| 83 | |
| 84 | // http://jsperf.com/hashing-strings |
| 85 | function hashString(string: string): number { |
| 86 | // This is the hash from JVM |
| 87 | // The hash code for a string is computed as |
| 88 | // s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1], |
| 89 | // where s[i] is the ith character of the string and n is the length of |
| 90 | // the string. We "mod" the result to make it between 0 (inclusive) and 2^31 |
| 91 | // (exclusive) by dropping high bits. |
| 92 | let hashed = 0; |
| 93 | for (let ii = 0; ii < string.length; ii++) { |
| 94 | hashed = (31 * hashed + string.charCodeAt(ii)) | 0; |
| 95 | } |
| 96 | return smi(hashed); |
| 97 | } |
| 98 | |
| 99 | // Per-process seed for the secondary collision hash. Never exposed nor |
| 100 | // serialized, so the public `hash()` stays deterministic. An odd base in |
no test coverage detected