Skip to content

Instantly share code, notes, and snippets.

@nuintun
Last active March 11, 2021 02:21
Show Gist options
  • Save nuintun/8b666793747100f1de1e8898982ce87e to your computer and use it in GitHub Desktop.
Save nuintun/8b666793747100f1de1e8898982ce87e to your computer and use it in GitHub Desktop.
Java 中字符串 hashCode 方法的 JavaScript 实现
/**
* @function hashCode
* @param {string} string
* @returns {number}
*/
function hashCode(string: string): number {
let hash: number = 0;
const { length }: string = string;
for (let i: number = 0; i < length; i++) {
const code: number = string.codePointAt(i) as number;
hash = (hash << 5) - hash + code;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment