Hyperlinkv0.8.0-beta.28

Equal

Equal.byReferenceUnsafeconsteffect/Equal.ts:547
<T extends object>(obj: T): T

Marks an object permanently to use reference equality, without creating a proxy.

When to use

Use when you need reference equality without proxy allocation and accept permanently marking the original object for reference-only equality.

Details

  • Adds obj to an internal WeakSet. From that point on, equals treats it as reference-only.
  • Returns the same object (not a copy or proxy), so byReferenceUnsafe(x) === x.
  • Does not affect the object's prototype, properties, or behavior beyond equality checks.

Gotchas

The marking is irreversible for the lifetime of the object.

Example (Marking an object for reference equality)

import { Equal } from "effect"

const obj1 = { a: 1, b: 2 }
const obj2 = { a: 1, b: 2 }

Equal.byReferenceUnsafe(obj1)

console.log(Equal.equals(obj1, obj2))   // false (reference)
console.log(Equal.equals(obj1, obj1))   // true  (same reference)
console.log(obj1 === Equal.byReferenceUnsafe(obj1)) // true (same object)
Source effect/Equal.ts:5474 lines
export const byReferenceUnsafe = <T extends object>(obj: T): T => {
  byReferenceInstances.add(obj)
  return obj
}
Referenced by 1 symbols