220

I'm working in JavaScript. I'd like to store a list of unique, unordered string values, with the following properties:

  1. a fast way to ask 'is A in the list'?
  2. a fast way to do 'delete A from the list if it exists in the list'
  3. a fast way to do 'add A to the list if it is not already present'.

What I really want is a set. Any suggestions for the best way to mimic a set in JavaScript?

This question recommends using an Object, with the keys storing properties, and the values all set to true: is that a sensible way?

2

7 Answers 7

263

If you are programming in an ES6-capable environment (such as node.js, a specific browser with the ES6 capabilities you need or transpiling ES6 code for your environment), then you can use the Set object built into ES6. It has very nice capabilities and can be used as is right in your environment.


For many simple things in an ES5 environment, using an Object works very well. If obj is your object and A is a variable that has the value you want to operate on in the set, then you can do these:

Initialization code:

// create empty object
var obj = {};

// or create an object with some items already in it
var obj = {"1":true, "2":true, "3":true, "9":true};

Question 1: Is A in the list:

if (A in obj) {
    // put code here
}

Question 2: Delete 'A' from the list if it's there:

delete obj[A];

Question 3: Add 'A' to the list if it wasn't already there

obj[A] = true;

For completeness, the test for whether A is in the list is a little safer with this:

if (Object.prototype.hasOwnProperty.call(obj, A))
    // put code here
}

because of potential conflict between built-in methods and/or properties on the base Object like the constructor property.


Sidebar on ES6: The current working version of ECMAScript 6 or somethings called ES 2015 has a built-in Set object. It is implemented now in some browsers. Since browser availability changes over time, you can look at the line for Set in this ES6 compatibility table to see the current status for browser availability.

One advantage of the built-in Set object is that it doesn't coerce all keys to a string like the Object does so you can have both 5 and "5" as separate keys. And, you can even use Objects directly in the set without a string conversion. Here's an article that describes some of the capabilities and MDN's documentation on the Set object.

I have now written a polyfill for the ES6 set object so you could start using that now and it will automatically defer to the built-in set object if the browser supports it. This has the advantage that you're writing ES6 compatible code that will work all the way back to IE7. But, there are some downsides. The ES6 set interface takes advantage of the ES6 iterators so you can do things like for (item of mySet) and it will automatically iterate through the set for you. But, this type of language feature cannot be implemented via polyfill. You can still iterate an ES6 set without using the new ES6 languages features, but frankly without the new language features, it isn't as convenient as the other set interface I include below.

You can decide which one works best for you after looking at both. The ES6 set polyfill is here: https://github.com/jfriend00/ES6-Set.

FYI, in my own testing, I've noticed that the Firefox v29 Set implementation is not fully up-to-date on the current draft of the spec. For example, you can't chain .add() method calls like the spec describes and my polyfill supports. This is probably a matter of a specification in motion as it is not yet finalized.


Pre-Built Set objects: If you want an already built object that has methods for operating on a set that you can use in any browser, you can use a series of different pre-built objects that implement different types of sets. There is a miniSet which is small code that implements the basics of a set object. It also has a more feature rich set object and several derivations including a Dictionary (let's you store/retrieve a value for each key) and an ObjectSet (let's you keep a set of objects - either JS objects or DOM objects where you either supply the function that generates a unique key for each one or the ObjectSet will generate the key for you).

Here's a copy of the code for the miniSet (most up-to-date code is here on github).

"use strict";
//-------------------------------------------
// Simple implementation of a Set in javascript
//
// Supports any element type that can uniquely be identified
//    with its string conversion (e.g. toString() operator).
// This includes strings, numbers, dates, etc...
// It does not include objects or arrays though
//    one could implement a toString() operator
//    on an object that would uniquely identify
//    the object.
// 
// Uses a javascript object to hold the Set
//
// This is a subset of the Set object designed to be smaller and faster, but
// not as extensible.  This implementation should not be mixed with the Set object
// as in don't pass a miniSet to a Set constructor or vice versa.  Both can exist and be
// used separately in the same project, though if you want the features of the other
// sets, then you should probably just include them and not include miniSet as it's
// really designed for someone who just wants the smallest amount of code to get
// a Set interface.
//
// s.add(key)                      // adds a key to the Set (if it doesn't already exist)
// s.add(key1, key2, key3)         // adds multiple keys
// s.add([key1, key2, key3])       // adds multiple keys
// s.add(otherSet)                 // adds another Set to this Set
// s.add(arrayLikeObject)          // adds anything that a subclass returns true on _isPseudoArray()
// s.remove(key)                   // removes a key from the Set
// s.remove(["a", "b"]);           // removes all keys in the passed in array
// s.remove("a", "b", ["first", "second"]);   // removes all keys specified
// s.has(key)                      // returns true/false if key exists in the Set
// s.isEmpty()                     // returns true/false for whether Set is empty
// s.keys()                        // returns an array of keys in the Set
// s.clear()                       // clears all data from the Set
// s.each(fn)                      // iterate over all items in the Set (return this for method chaining)
//
// All methods return the object for use in chaining except when the point
// of the method is to return a specific value (such as .keys() or .isEmpty())
//-------------------------------------------


// polyfill for Array.isArray
if(!Array.isArray) {
    Array.isArray = function (vArg) {
        return Object.prototype.toString.call(vArg) === "[object Array]";
    };
}

function MiniSet(initialData) {
    // Usage:
    // new MiniSet()
    // new MiniSet(1,2,3,4,5)
    // new MiniSet(["1", "2", "3", "4", "5"])
    // new MiniSet(otherSet)
    // new MiniSet(otherSet1, otherSet2, ...)
    this.data = {};
    this.add.apply(this, arguments);
}

MiniSet.prototype = {
    // usage:
    // add(key)
    // add([key1, key2, key3])
    // add(otherSet)
    // add(key1, [key2, key3, key4], otherSet)
    // add supports the EXACT same arguments as the constructor
    add: function() {
        var key;
        for (var i = 0; i < arguments.length; i++) {
            key = arguments[i];
            if (Array.isArray(key)) {
                for (var j = 0; j < key.length; j++) {
                    this.data[key[j]] = key[j];
                }
            } else if (key instanceof MiniSet) {
                var self = this;
                key.each(function(val, key) {
                    self.data[key] = val;
                });
            } else {
                // just a key, so add it
                this.data[key] = key;
            }
        }
        return this;
    },
    // private: to remove a single item
    // does not have all the argument flexibility that remove does
    _removeItem: function(key) {
        delete this.data[key];
    },
    // usage:
    // remove(key)
    // remove(key1, key2, key3)
    // remove([key1, key2, key3])
    remove: function(key) {
        // can be one or more args
        // each arg can be a string key or an array of string keys
        var item;
        for (var j = 0; j < arguments.length; j++) {
            item = arguments[j];
            if (Array.isArray(item)) {
                // must be an array of keys
                for (var i = 0; i < item.length; i++) {
                    this._removeItem(item[i]);
                }
            } else {
                this._removeItem(item);
            }
        }
        return this;
    },
    // returns true/false on whether the key exists
    has: function(key) {
        return Object.prototype.hasOwnProperty.call(this.data, key);
    },
    // tells you if the Set is empty or not
    isEmpty: function() {
        for (var key in this.data) {
            if (this.has(key)) {
                return false;
            }
        }
        return true;
    },
    // returns an array of all keys in the Set
    // returns the original key (not the string converted form)
    keys: function() {
        var results = [];
        this.each(function(data) {
            results.push(data);
        });
        return results;
    },
    // clears the Set
    clear: function() {
        this.data = {}; 
        return this;
    },
    // iterate over all elements in the Set until callback returns false
    // myCallback(key) is the callback form
    // If the callback returns false, then the iteration is stopped
    // returns the Set to allow method chaining
    each: function(fn) {
        this.eachReturn(fn);
        return this;
    },
    // iterate all elements until callback returns false
    // myCallback(key) is the callback form
    // returns false if iteration was stopped
    // returns true if iteration completed
    eachReturn: function(fn) {
        for (var key in this.data) {
            if (this.has(key)) {
                if (fn.call(this, this.data[key], key) === false) {
                    return false;
                }
            }
        }
        return true;
    }
};

MiniSet.prototype.constructor = MiniSet;
24
  • 16
    This solves the question, but to be clear, this implementation won't work for sets of things besides integer or strings.
    – mkirk
    Commented Nov 13, 2012 at 9:43
  • 3
    @mkirk - yes the item you are indexing in the set must have a string representation that can be the index key (e.g. it either is a string or has a toString() method that uniquely describes the item).
    – jfriend00
    Commented Nov 13, 2012 at 13:46
  • 4
    To get the items in the list, you can use Object.keys(obj).
    – Blixt
    Commented Dec 5, 2012 at 16:05
  • 3
    @Blixt - Object.keys() needs IE9, FF4, Safari 5, Opera 12 or higher. There's a polyfill for older browsers here.
    – jfriend00
    Commented Dec 5, 2012 at 16:46
  • 1
    Don't use obj.hasOwnProperty(prop) for membership checks. Use Object.prototype.hasOwnProperty.call(obj, prop) instead, which works even if the "set" contains the value "hasOwnProperty". Commented Jul 11, 2013 at 22:33
72

You can create an Object with no properties like

var set = Object.create(null)

which can act as a set and eliminates the need to use hasOwnProperty.


var set = Object.create(null); // create an object with no properties

if (A in set) { // 1. is A in the list
  // some code
}
delete set[a]; // 2. delete A from the list if it exists in the list 
set[A] = true; // 3. add A to the list if it is not already present
11
  • Nice, but not sure why you say that "eliminates the need to use hasOwnProperty"
    – blueFast
    Commented Feb 7, 2014 at 10:26
  • 13
    If you just use set = {} it will inherit all the properties from Object (e.g. toString), so you will have to check for the payload of the set (properties you added) with hasOwnProperty in if (A in set) Commented Feb 7, 2014 at 10:31
  • 6
    I did not know that it is possible to create a completely empty object. Thanks, your solution is very elegant.
    – blueFast
    Commented Feb 7, 2014 at 10:51
  • 1
    Interesting, but the downside of this is surely that you have to have set[A]=true statements for every element you wish to add instead of just one initialiser?
    – vogomatix
    Commented Jun 19, 2014 at 12:14
  • 1
    Not sure what you mean, but if you are referring to initialising a set by an already present set, you can do something along the lines of s = Object.create(null);s["thorben"] = true;ss = Object.create(s) Commented Jun 19, 2014 at 20:39
23

As of ECMAScript 6, the Set data-structure is a built-in feature. Compatibility with node.js versions can be found here.

6
  • 4
    Hello, just for clarity - it's 2014 now, is this still experimental in Chrome? If it is not, could you please edit your answer? Thanks Commented Feb 17, 2014 at 1:08
  • 1
    Yes, it is still experimental for Chrome. I believe that by the end of 2014, when ECMAScript is supposed to be 'officially' released, it will be supported. I'll then update my answer accordingly.
    – hymloth
    Commented Feb 17, 2014 at 10:08
  • OK, thanks for answering! (JavaScript answers get outdated pretty quickly.) Commented Feb 17, 2014 at 18:11
  • 1
    @Val in doesn't work because Set objects doesn't have its elements as properties, which would be bad because sets can have elements of any type, but properties are strings. You can use has: Set([1,2]).has(1)
    – Oriol
    Commented Jul 26, 2014 at 22:07
  • 1
    Salvador Dali's answer is more comprehensive and up-to-date. Commented Jun 6, 2016 at 23:49
15

In ES6 version of Javascript you have built in type for set (check compatibility with your browser).

var numbers = new Set([1, 2, 4]); // Set {1, 2, 4}

To add an element to the set you simply use .add(), which runs in O(1) and either adds the element to set (if it does not exist) or does nothing if it is already there. You can add element of any type there (arrays, strings, numbers)

numbers.add(4); // Set {1, 2, 4}
numbers.add(6); // Set {1, 2, 4, 6}

To check the number of elements in the set, you can simply use .size. Also runs in O(1)

numbers.size; // 4

To remove the element from the set use .delete(). It returns true if the value was there (and was removed), and false if the value did not exist. Also runs in O(1).

numbers.delete(2); // true
numbers.delete(2); // false

To check whether the element exist in a set use .has(), which returns true if the element is in the set and false otherwise. Also runs in O(1).

numbers.has(3); // false
numbers.has(1); // true

In addition to methods you wanted, there are few additional one:

  • numbers.clear(); would just remove all elements from the set
  • numbers.forEach(callback); iterating through the values of the set in insertion order
  • numbers.entries(); create an iterator of all the values
  • numbers.keys(); returns the keys of the set which is the same as numbers.values()

There is also a Weakset which allows to add only object-type values.

1
  • could you point a reference to .add() runs in O(1)? I am intrigued by this,
    – Green
    Commented Apr 20, 2016 at 3:34
10

I have started an implementation of Sets that currently works pretty well with numbers and strings. My main focus was the difference operation, so I tried to make it as efficient as I could. Forks and code reviews are welcome!

https://github.com/mcrisc/SetJS

1
  • wow this class is nuts! I would totally use this if I wasn't writing JavaScript inside CouchDB map/reduce functions!
    – benathon
    Commented Sep 3, 2012 at 7:13
9

I just noticed that d3.js library has implementation of sets, maps and other data structures. I can't argue about their efficiency but judging by the fact that it is a popular library it must be what you need.

The documentation is here

For convenience I copy from the link (the first 3 functions are those of interest)


  • d3.set([array])

Constructs a new set. If array is specified, adds the given array of string values to the returned set.

  • set.has(value)

Returns true if and only if this set has an entry for the specified value string.

  • set.add(value)

Adds the specified value string to this set.

  • set.remove(value)

If the set contains the specified value string, removes it and returns true. Otherwise, this method does nothing and returns false.

  • set.values()

Returns an array of the string values in this set. The order of the returned values is arbitrary. Can be used as a convenient way of computing the unique values for a set of strings. For example:

d3.set(["foo", "bar", "foo", "baz"]).values(); // "foo", "bar", "baz"

  • set.forEach(function)

Calls the specified function for each value in this set, passing the value as an argument. The this context of the function is this set. Returns undefined. The iteration order is arbitrary.

  • set.empty()

Returns true if and only if this set has zero values.

  • set.size()

Returns the number of values in this set.

4

Yes, that's a sensible way--that's all an object is (well, for this use-case)--a bunch of keys/values with direct access.

You'd need to check to see if it's already there before adding it, or if you just need to indicate presence, "adding" it again doesn't actually change anything, it just sets it on the object again.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.