(str: string): numberComputes 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")) // trueexport const const string: (str: string) => numberComputes 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
string = (str: stringstr: string) => {
let let h: numberh = 5381, let i: numberi = str: stringstr.String.length: numberReturns the length of a String object.
length
while (let i: numberi) {
let h: numberh = (let h: numberh * 33) ^ str: stringstr.String.charCodeAt(index: number): numberReturns the Unicode value of the character at the specified location.
charCodeAt(--let i: numberi)
}
return const optimize: (n: number) => numberApplies bit manipulation techniques to optimize a hash value.
When to use
Use to improve the bit distribution of a raw numeric hash value.
Details
This function takes a hash value and applies bitwise operations to improve
the distribution of hash values, reducing the likelihood of collisions.
Example (Optimizing a hash value)
import { Hash } from "effect"
const rawHash = 1234567890
const optimizedHash = Hash.optimize(rawHash)
console.log(optimizedHash) // optimized hash value
// Often used internally by other hash functions
const stringHash = Hash.optimize(Hash.string("hello"))
optimize(let h: numberh)
}