Map vs IterableMap

JavaScript performance comparison

Test case created

Preparation code

<script>
/**
 * A wrapper for a harmony Map that is iterable.
 * It does not allow deletion of keys however.
 */

function IterableMap() {
  this._internal = new Map();
  this._keys = [];
}

IterableMap.prototype.has = function(key) {
  return this._internal.has(key);
};

IterableMap.prototype.get = function(key) {
  return this._internal.get(key);
};

IterableMap.prototype.set = function(key, val) {
  var internal = this._internal;
  if(!internal.has(key)) {
    this._keys.push(key);
  }
  return internal.set(key, val);
};

IterableMap.prototype.delete = function(key) {
  return this._internal.delete(key);
};

IterableMap.prototype.forEach = function(cb, thisObj) {
  var keys = this._keys,
    internal = _internal;
  for(var i = 0, il = keys.length; i < il; i++) {
    var key = keys[i];
    cb.call(thisObj, internal.get(key), key, this);
  }
};

Object.defineProperty(IterableMap.prototype, "size", {
  get: function() { return this._internal.size; }
});
</script>
 
<script>
Benchmark.prototype.setup = function() {
    var keys = [
      1,
      {},
      [],
      "1"
    ];
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Map
var map = new Map();

for(var i = 0, il = vals.length; i < il; i++) {
   map.set(vals[i], 1);
   map.has(vals[i]);
   map.get(vals[i]);
}
pending…
IterableMap
var map = new IterableMap();

for(var i = 0, il = vals.length; i < il; i++) {
   map.set(vals[i], 1);
   map.has(vals[i]);
   map.get(vals[i]);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment