(n: number): numberComputes a hash value for a number.
When to use
Use to hash a JavaScript number with Effect's numeric hash semantics.
Details
This function creates a hash value for numeric inputs, handling special cases like NaN, Infinity, and -Infinity with distinct hash values. It uses bitwise operations to ensure good distribution of hash values across different numeric inputs.
Example (Hashing numbers)
import { Hash } from "effect"
console.log(Hash.number(42)) // hash of 42
console.log(Hash.number(3.14)) // hash of 3.14
console.log(Hash.number(NaN)) // hash of "NaN"
console.log(Hash.number(Infinity)) // 0 (special case)
// Same numbers produce the same hash
console.log(Hash.number(100) === Hash.number(100)) // trueexport const const number: (n: number) => numberComputes a hash value for a number.
When to use
Use to hash a JavaScript number with Effect's numeric hash semantics.
Details
This function creates a hash value for numeric inputs, handling special cases
like NaN, Infinity, and -Infinity with distinct hash values. It uses bitwise operations to ensure good distribution
of hash values across different numeric inputs.
Example (Hashing numbers)
import { Hash } from "effect"
console.log(Hash.number(42)) // hash of 42
console.log(Hash.number(3.14)) // hash of 3.14
console.log(Hash.number(NaN)) // hash of "NaN"
console.log(Hash.number(Infinity)) // 0 (special case)
// Same numbers produce the same hash
console.log(Hash.number(100) === Hash.number(100)) // true
number = (n: numbern: number) => {
if (n: numbern !== n: numbern) {
return 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("NaN")
}
if (n: numbern === var Infinity: numberInfinity) {
return 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("Infinity")
}
if (n: numbern === -var Infinity: numberInfinity) {
return 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("-Infinity")
}
let let h: numberh = n: numbern | 0
if (let h: numberh !== n: numbern) {
let h: numberh ^= n: numbern * 0xffffffff
}
while (n: numbern > 0xffffffff) {
let h: numberh ^= n: numbern /= 0xffffffff
}
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)
}