MCPcopy Create free account
hub / github.com/immutable-js/immutable-js / hashString

Function hashString

src/Hash.ts:85–97  ·  view source on GitHub ↗
(string: string)

Source from the content-addressed store, hash-verified

83
84// http://jsperf.com/hashing-strings
85function 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

Callers 2

hashFunction · 0.85
cachedHashStringFunction · 0.85

Calls 1

smiFunction · 0.90

Tested by

no test coverage detected