Maybe it's just my own ignorance not being familiar with the Set syntax. But the Array.filter method is so widely used that I'd know instantly what that line did. The example in the original post would confuse me to no end if there were no comments. I'd definitely have to google to figure out what's going on and why.
I'm sure the syntax is nice when you're familiar with it, but it's just not something that's done very often, so I'd definitely always leave a comment before it if you're using it in a code base that other people will have to read eventually.
I don't think the Array.filter + Array.indexOf approach is more performant. Unless I'm missing something.
Both are linear, so for every item you are filtering, you are also iterating over the array again to find an index. Therefore, in the worst case, it will end up having a quadratic O(n^2) performance, which is really bad.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Infact, Array.filter() is more performant but it's a bit harder to understand it's working when compared to this.
If performance isn't critical then the method in the post is more readable and simpler.
Maybe it's just my own ignorance not being familiar with the Set syntax. But the Array.filter method is so widely used that I'd know instantly what that line did. The example in the original post would confuse me to no end if there were no comments. I'd definitely have to google to figure out what's going on and why.
I'm sure the syntax is nice when you're familiar with it, but it's just not something that's done very often, so I'd definitely always leave a comment before it if you're using it in a code base that other people will have to read eventually.
I agree, Set isn't something we commonly use.
I don't think the
Array.filter
+Array.indexOf
approach is more performant. Unless I'm missing something.Both are linear, so for every item you are filtering, you are also iterating over the array again to find an index. Therefore, in the worst case, it will end up having a quadratic
O(n^2)
performance, which is really bad.