A Set (as in math) implementation in JavaScript
JavaScript CSS HTML
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/jasmine-1.1.0
spec
src
.gitignore
MIT.LICENSE
README.md
SpecRunner.html

README.md

SetJS

SetJS is a pure-JavaScript implementation of the set concept aimed at efficient execution of difference (complement) and intersection operations.

Usage Examples

var res;

var a = new Set([32, 1, 5, 15, 19, 20, 20, 20, 20, 20]);
print(a);
// "Set [1, 5, 15, 19, 20, 32]"

var b = new Set([5, 15, 16, 20, 25]);
print(b);
// "Set [5, 15, 16, 20, 25]";


res = b.difference(a);
print(res);
// "Set [16, 25]";

res = b.intersection(a);
print(res);
// "Set [5, 15, 20]"

res.add(40);
print(res);
// "Set [5, 15, 20, 40]"

Supported Operations

The following common set operations are supported by SetJS:

  • Membership: if (a.contains(x)) {...}
  • Equality: if (a.equals(b)) {...}
  • Union: res = a.union(b);
  • Difference: res = b.difference(a);
  • Intersection: res = b.intersection(a);

Set objects in ECMAScript 2015 (ES6)

At the time SetJS was released (2011) JavaScript (ECMAScript) lacked a set object. The latest version of ECMAScript standard (ES6) introduced set objects to JavaScript. But these are still being implemented by some browsers. Besides that, ES6 sets yet does not support some set operations.

Bellow there's a brief comparison of SetJS and ES6 set objects main features:

Operation SetJS ES6
iteration thru elements no yes
delete elements no yes
membership yes yes
equality yes no
difference yes no
intersection yes no