Skip to content

Instantly share code, notes, and snippets.

@avioli
Created March 8, 2024 03:46
Show Gist options
  • Save avioli/23b6b9cbcbbab12e657c67aa970b3970 to your computer and use it in GitHub Desktop.
Save avioli/23b6b9cbcbbab12e657c67aa970b3970 to your computer and use it in GitHub Desktop.
Fast hash function in Dart
/// FNV-1a 64bit hash algorithm optimized for Dart Strings
int fastHash(String string) {
var hash = 0xcbf29ce484222325;
var i = 0;
while (i < string.length) {
final codeUnit = string.codeUnitAt(i++);
hash ^= codeUnit >> 8;
hash *= 0x100000001b3;
hash ^= codeUnit & 0xFF;
hash *= 0x100000001b3;
}
return hash;
}
// From: https://isar.dev/recipes/string_ids.html#fast-hash-function
// Avoid using string.hashCode because it is not guaranteed to be stable across different platforms and versions of Dart.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment