object-map

JavaScript performance comparison

Test case created

Preparation code

<script>
  function ObjectMap() {
    this.key = 0;
    this.mapId = '_' + Math.floor(Math.random() * 10000);
    this.data = {};
  }

  ObjectMap.prototype = {
    set: function(object, value) {
      var key = ++this.key;

      if (object[this.mapId]) {
        return;
      }

      object[this.mapId] = key;
      this.data[key] = value;
    },

    get: function(object) {
      var key = object[this.mapId];

      return key ? this.data[key] : null;
    },

    remove: function(object) {
      var key = object[this.mapId];
      if (!key) {
        return;
      }
      delete this.data[key];
      delete object[key];
    }
  };

  function a() {
    //some code
    //some code
    //some code
    //some code
    //some code
    //some code
    //some code
    //some code
  }
</script>
<script>
Benchmark.prototype.setup = function() {
    var m1 = {},
        m2 = new ObjectMap(),
        m3 = {};
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
object
m1[a] = 'test';
m1[a];
delete m1[a];
pending…
ObjectMap
m2.set(a, 'test');
m2.get(a);
m2.remove(a);
pending…
object.toString()
m3[a.toString()] = 'test';
m3[a.toString()];
delete m3[a.toString()];
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment