Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created May 2, 2025 15:01
Show Gist options
  • Save colelawrence/1b08beb89096f2328042b358242522ca to your computer and use it in GitHub Desktop.
Save colelawrence/1b08beb89096f2328042b358242522ca to your computer and use it in GitHub Desktop.
Hash string
/**
* FNV-1a 32-bit hash – fast, non-cryptographic, browser-safe.
* Sufficient for generating repeatable fragment keys.
*/
export const fnv1aHash32 = (str: string): string => {
let h = 0x811c9dc5; // FNV offset basis
for (let i = 0; i < str.length; i++) {
h ^= str.charCodeAt(i);
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
// Convert to unsigned 32-bit hex and pad to 8 chars
return (h >>> 0).toString(16).padStart(8, '0');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment