(self: object, that: object, f: (a: any, b: any) => boolean)
| 288 | } |
| 289 | |
| 290 | function withCache(self: object, that: object, f: (a: any, b: any) => boolean): boolean { |
| 291 | // Check cache first |
| 292 | let selfMap = equalityCache.get(self) |
| 293 | if (!selfMap) { |
| 294 | selfMap = new WeakMap() |
| 295 | equalityCache.set(self, selfMap) |
| 296 | } else if (selfMap.has(that)) { |
| 297 | return selfMap.get(that)! |
| 298 | } |
| 299 | |
| 300 | // Perform the comparison |
| 301 | const result = f(self, that) |
| 302 | |
| 303 | // Cache the result bidirectionally |
| 304 | selfMap.set(that, result) |
| 305 | |
| 306 | let thatMap = equalityCache.get(that) |
| 307 | if (!thatMap) { |
| 308 | thatMap = new WeakMap() |
| 309 | equalityCache.set(that, thatMap) |
| 310 | } |
| 311 | thatMap.set(self, result) |
| 312 | |
| 313 | return result |
| 314 | } |
| 315 | |
| 316 | const equalityCache = new WeakMap<object, WeakMap<object, boolean>>() |
| 317 |
no test coverage detected