Skip to content

Commit

Permalink
Refactor: optimize ArrayPoly.filterUniqueMapped() (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Jun 11, 2024
1 parent 273ecca commit e2d7d78
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/polyfill/arrayPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ export default class ArrayPoly {
public static filterUniqueMapped<T, V>(
mapper: (arg: T) => V,
): (value: T, idx: number, values: T[]) => boolean {
let mappedValues: V[] | undefined;
const mappedValues = new Map<V, number>();
return (value: T, idx: number, values: T[]): boolean => {
if (!mappedValues) {
mappedValues = values.map((val) => mapper(val));
if (mappedValues.size === 0) {
values.forEach((val, valIdx) => {
const mapped = mapper(val);
if (!mappedValues.has(mapped)) {
mappedValues.set(mapped, valIdx);
}
});
}
return mappedValues.indexOf(mapper(value)) === idx;
return mappedValues.get(mapper(value)) === idx;
};
}

Expand Down

0 comments on commit e2d7d78

Please sign in to comment.