Skip to content

Instantly share code, notes, and snippets.

@codemile
Created January 2, 2025 16:49
Show Gist options
  • Save codemile/3f1595e5f355697332eed05b05af547c to your computer and use it in GitHub Desktop.
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.
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