DEV Community

Discussion on: Detecting unique arrays in Javascript

Collapse
 
ogonommo profile image
Stoyan Peshev

I would also consider how those three approaches handle different type values.

For example Approach 2 will not handle objects well (objects as computed properties), and will throw if you pass something that can't be converted to a string, i.e. Object.create(null). Approach 1 will fail with NaN values, it will falsely report [NaN, NaN] as unique.

Approach 3 is the one I would use, or a mix of Approach 2 and 3 (using a set, or preferably a weak set as a caching tool).