Skip to content

Commit

Permalink
Merge pull request #47 from jtrim-ons/faster-uniques
Browse files Browse the repository at this point in the history
Use a Set to store seen values in unique(), for speed
  • Loading branch information
mhkeller committed Jun 30, 2021
2 parents bc13c6c + aa44411 commit b7f77ad
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/lib/uniques.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ export default function uniques (list, iteratee, transform = true) {
const ll = list.length;
const iterater = typeof iteratee === 'function';
const key = typeof iteratee !== 'undefined';
const seen = [];
const seen = new Set();
const result = [];
for (let i = 0; i < ll; i += 1) {
const d = list[i];
const computed = iterater ? iteratee(d) : key === true ? d[iteratee] : d;
if (!seen.includes(computed)) {
seen.push(computed);
if (transform === false) {
result.push(d);
}
if (!seen.has(computed)) {
seen.add(computed);
result.push(transform ? computed : d);
}
}
return transform === false ? result : seen;
return result;
}

0 comments on commit b7f77ad

Please sign in to comment.