MCPcopy Create free account
hub / github.com/Effect-TS/effect / compareObjects

Function compareObjects

packages/effect/src/Equal.ts:212–255  ·  view source on GitHub ↗
(self: object, that: object)

Source from the content-addressed store, hash-verified

210}
211
212function compareObjects(self: object, that: object): boolean {
213 const depth = pathLeft.length
214 const prev = seen.get(self)
215 if (prev === that) return true
216 let known: WeakMap<object, boolean> | undefined
217 if (!depth || prev !== undefined) {
218 known = results.get(self)
219 const cached = known?.get(that)
220 if (cached !== undefined) return cached
221 }
222 let revisited = false
223 for (let i = depth; i-- > 0;) {
224 const sameLeft = pathLeft[i] === self
225 if (sameLeft || pathRight[i] === that) {
226 addBackEdge()
227 // A repeated pair closes a cycle under coinductive equality.
228 if (sameLeft && pathRight[i] === that) return true
229 revisited = true
230 }
231 }
232 // Reject mismatched cycle lengths instead of unrolling both loops.
233 if (revisited && Hash.hash(self) !== Hash.hash(that)) return false
234 const seenBack = backEdges
235 pathLeft.push(self)
236 pathRight.push(that)
237 let result: boolean
238 try {
239 result = compareStructure(self, that)
240 } finally {
241 pathLeft.pop()
242 pathRight.pop()
243 if (!depth) seen.clear()
244 }
245 // Cyclic hashes depend on entry point; verify hash agreement.
246 if (seenBack !== backEdges) return result && Hash.hash(self) === Hash.hash(that)
247 // Only cache acyclic results so nested lookups never skip a back-edge.
248 if (depth && prev === undefined) {
249 if (result) seen.set(self, that)
250 } else {
251 if (!known) results.set(self, known = new WeakMap())
252 known.set(that, result)
253 }
254 return result
255}
256
257// The pairs on the current comparison's path (the coinductive assumptions).
258const pathLeft: Array<object> = []

Callers 1

compareBothFunction · 0.85

Calls 6

addBackEdgeFunction · 0.90
compareStructureFunction · 0.85
getMethod · 0.65
pushMethod · 0.65
clearMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected