Hyperlinkv0.8.0-beta.28

Hash

Hash.randomconsteffect/Hash.ts:195
<A extends object>(self: A): number

Generates a random hash value for an object and caches it.

When to use

Use to hash an object by reference identity instead of structural content.

Details

This function creates a random hash value for objects that don't have their own hash implementation. The hash value is cached using a WeakMap, so the same object will always return the same hash value during its lifetime.

Example (Hashing objects by reference)

import { Hash } from "effect"

const obj1 = { a: 1 }
const obj2 = { a: 1 }

// Same object always returns the same hash
console.log(Hash.random(obj1) === Hash.random(obj1)) // true

// Different objects get different hashes
console.log(Hash.random(obj1) === Hash.random(obj2)) // false
hashing
Source effect/Hash.ts:1956 lines
export const random: <A extends object>(self: A) => number = (self) => {
  if (!randomHashCache.has(self)) {
    randomHashCache.set(self, number(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)))
  }
  return randomHashCache.get(self)!
}
Referenced by 1 symbols