Set polyfill with a "has" method more efficient than O(n)

Peter Michaux petermichaux at gmail.com
Thu Mar 29 22:17:36 PDT 2012


I've worked on a generic Set polyfill. It is quite a simple task to
build one but determining if an object is in the set is O(n) with the
following "has" method.

    Set.prototype.has = function(element) {
        for (var i = 0, ilen = this._elements.length; i < ilen; i++) {
            if (element === this._elements[i]) {
                return true;
            }
        }
        return false;
    };

It seems like a long shot but is there some trick that someone has
discovered that allows for a more efficient generic Set polyfill?

Thanks,
Peter


More information about the es-discuss mailing list