Skip to content

Instantly share code, notes, and snippets.

@avioli
Created March 8, 2024 03:46
Show Gist options
  • Select an option

  • Save avioli/23b6b9cbcbbab12e657c67aa970b3970 to your computer and use it in GitHub Desktop.

Select an option

Save avioli/23b6b9cbcbbab12e657c67aa970b3970 to your computer and use it in GitHub Desktop.

Revisions

  1. avioli created this gist Mar 8, 2024.
    18 changes: 18 additions & 0 deletions fast_hash.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    /// 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.