Skip to content

Instantly share code, notes, and snippets.

@rsms
Created August 6, 2015 21:25
Show Gist options
  • Save rsms/2d01d7f9f5159a7a7d1f to your computer and use it in GitHub Desktop.
Save rsms/2d01d7f9f5159a7a7d1f to your computer and use it in GitHub Desktop.
ES6 Set equality test
function seteq(a, b) {
if (a != b) {
if (!a || !b) { return false; }
for (var e of a) {
if (!b.has(e)) { return false; }
}
}
return true;
}
var assert = require('assert');
var s1 = Symbol(), s2 = Symbol();
var a = new Set([s1]), b = new Set([s2]);
assert(seteq(a, b) === false);
assert(seteq(a, null) === false);
assert(seteq(null, b) === false);
assert(seteq(null, null) === true);
assert(seteq(null, undefined) === true);
a.add(s2);
assert(seteq(a, b) === false);
b.add(s1);
assert(seteq(a, b) === true);
@darknoon
Copy link

darknoon commented Apr 11, 2017

A check that the sets are of same size would greatly help your comparison's accuracy without needing to loop over them in the case they have different numbers of items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment