Created
January 2, 2025 16:49
-
-
Save codemile/3f1595e5f355697332eed05b05af547c to your computer and use it in GitHub Desktop.
A TypeScript utility function to generate a 32-bit hash for a given string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function hashString(str: string): number { | |
let hash = 0; | |
for (let i = 0; i < str.length; i++) { | |
hash = (hash << 5) - hash + str.charCodeAt(i); | |
hash |= 0; // Convert to 32-bit integer | |
} | |
return Math.abs(hash); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment