Reputation: 4533
I have this two functions which does the same thing, to insert only unique element into a set. Just wondering, how is the performance difference is it checks the element before insert verses without?
function removeAllDup(n) {
// Set (ES6) is a collection for unique values.
let seen = new Set;
n.forEach(item => seen.add(item));
return seen;
}
function removeAllDup2(n) {
// Set (ES6) is a collection for unique values.
let seen = new Set;
n.forEach(element => {
// if element does not exist in the set, add the element.
if (!seen.has(element)) {
seen.add(element);
}
})
return seen;
}
Upvotes: 0
Views: 37