You should be aware, that the different options have huge performance implications. It is relevant how big the array is and how many different elements are in the array. filter
has a complexity of O(n^2)
since indexOf
is also O(n)
in the worst case. The reduce
way grows as you have more different elements in the array. A quick test in node.js results in following:
Array length 10^9
and 100
different elements: Filter 150ms, Reduce 90ms, Set 20ms
Array length 10^6 and
10^5
different elements: Filter 105.987ms, Reduce 116.856ms, Set 53ms.
This is a gigantic difference that might crash your application if you have bigger arrays than in you test sizes.