You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Should be self-explanatory.

    Honestly, I'm not finding it that easy to see what's going on there :) So you've got promises as map keys?

    Here's a bodged up Map implementation:

    function Map(keyVal) {
      this._keys = [];
      this._values = [];
      if (keyVal) keyVal.forEach(k=>{
        this._keys.push(k[0]);
        this._values.push(k[1]);
      });
      this.size = this._keys.length;
    }
    Map.prototype.has = function(k) {
      return this._keys.indexOf(k)>=0;
    };
    Map.prototype.get = function(k) {
      var i = this._keys.indexOf(k);
      if (i<0) return;
      return this._values[i];
    };
    Map.prototype.set = function(k,v) {
      var i = this._keys.indexOf(k);
      if (i<0) {
        this._keys.push(k);
        this._values.push(v);
        this.size = this._keys.length;
      } else this._values[i] = v;
      return this;
    };
    Map.prototype.delete = function(k) {
      var i = this._keys.indexOf(k);
      if (i<0) return false;
      this._keys.splice(i,1);
      this._values.splice(i,1);
      this.size = this._keys.length;
    };
    Map.prototype.keys = function() { return this._keys; };
    Map.prototype.values = function() { return this._values; };
    

    It's actually pretty quick because it uses Array.indexOf. It doesn't work for NaN (that could be special-cased) but I think it works for most other stuff.

    I've added it to the ES6 wishlist linked above, but looking at the API I wonder if it makes much sense without Iterator support?

About

Avatar for Gordon @Gordon started