However even the latter comparison is aimed for simple and small arrays as it can be pretty expensive and yield unexpected results for some edge-cases, e.g.:
JSON.stringify(newSet([1,2,3]))// '{}'
In those cases one can use deep equal implementation from a variety of packages.
There is no general right or wrong as it depends on a particular use case, but if you want an optimal solution that would handle most of the cases it'll probably be a recursive iterator over indices with early return.
This method will not work for non-primitive values:
Although slight alteration to the algorithm will do the trick:
However even the latter comparison is aimed for simple and small arrays as it can be pretty expensive and yield unexpected results for some edge-cases, e.g.:
In those cases one can use
deep equal
implementation from a variety of packages.true, most short hacks usually have drawbacks
Your solution has too many drawbacks.
That was not very constructive really. Care to explain a bit what the drawbacks could be?
Last time I needed to compare sommeting in JS I used "JSON.stringify" but its still a hack. What is the correct way to do it nowadays? 🤔
Writing an algorithm with loop and recursion if needed
Extractly
There is no general right or wrong as it depends on a particular use case, but if you want an optimal solution that would handle most of the cases it'll probably be a recursive iterator over indices with early return.
I read that as "non-prime values" at first and was severely confused 😆
also you can use lodash.isMatch
_.isMatch