Created
March 8, 2024 03:46
-
-
Save avioli/23b6b9cbcbbab12e657c67aa970b3970 to your computer and use it in GitHub Desktop.
Fast hash function in Dart
This file contains 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
/// 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