Skip to content

resolveEqualityComparator

resolveEqualityComparator<A, B>(comparisonStrategy?): EqualityComparator<A, B>

Defined in: packages/core/src/object/is-equal.ts:146

Resolves an EqualityComparisonStrategy to the comparator it names, once, so a per-element caller does not re-dispatch the string on every comparison.

isEqual re-reads comparisonStrategy and re-enters its switch per call, which is fine for a one-shot comparison and wrong inside a loop that runs once per element. Resolve at construction (or at the top of the operation) and call the result.

A

B

EqualityComparisonStrategy<A, B> = "strict"

The strategy to resolve. Defaults to "strict".

EqualityComparator<A, B>

The comparator. A custom predicate is returned as-is; a well-known strategy resolves to a shared function, never a fresh closure.

const equals = resolveEqualityComparator(options.comparisonStrategy);
for (let i = 0; i < items.length; i++) {
if (equals(items[i]!, needle)) return i;
}
```;