First, this approach will not work for non-primitive values. If you're dealing with an array of arrays or an array of objects or even an array of functions, it'll just compare their references. So if the array has two objects that have the same contents but were declared separately, you'll still get values that look like duplicates. That might be just fine for some applications, but if it's not, you'll have to do a deeper comparison.
Second, is this actually better? This isn't just rhetorical; I'm actually asking. Filtering is O(n), but so is indexOf, so since the indexOf call is inside the filter, the whole operation is O(n2). Meanwhile, generating a set is also O(n), as is destructing it into an array. But since they're done back-to-back and not inside each other, it's O(2n). I know that the highest factor doesn't always tell the whole story, but are there times when the set approach would perform better?
20
u/slowthedataleak May 14 '21
Does this perform better than filter()?