Hyperlinkv0.8.0-beta.28

Hash

Hash.stringconsteffect/Hash.ts:386
(str: string): number

Computes a hash value for a string using the djb2 algorithm.

When to use

Use when you need a string field to contribute to a custom structural hash implementation.

Details

This function implements a variation of the djb2 hash algorithm, which is known for its good distribution properties and speed. It processes each character of the string to produce a consistent hash value.

Example (Hashing strings)

import { Hash } from "effect"

console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string

// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
hashing
Source effect/Hash.ts:3867 lines
export const string = (str: string) => {
  let h = 5381, i = str.length
  while (i) {
    h = (h * 33) ^ str.charCodeAt(--i)
  }
  return optimize(h)
}
Referenced by 4 symbols