I've written some code that uses ECMAScript 6 Set
s in various recursive functions, to keep track of objects that have already been encountered (thereby avoiding infinite recursion); e.g.
function traverse(start, callback, next) {
var seen = new Set();
function _traverse(obj) {
if (seen.has(obj)) { return }
seen.add(obj);
callback(obj);
_traverse(next(obj));
}
return _traverse(start);
}
I prefer to use these Set
s for this purpose over plain Object
s, because the former distinguish between, e.g., between the integer 0
and the string "0"
.
It is my understanding, however, that browser support for such Set
s may not yet be widespread enough for production code. Therefore, I'm looking for a more portable replacement with equal functionality (such as, e.g., distinguishing 0
and "0"
, as described above).