long prototype chains (v3)

Revision 3 of this benchmark created by Forrest L Norvell on


Description

Which is the fastest way to create objects with hierarchical property lookup?

Setup

function Map(parent) {
      this.__parent = parent;
    }
    Map.prototype = Object.create(null);
    Map.prototype.get = function (key) {
      var current = this;
      do {
        if (key in current) return current[key];
      } while ((current = current.__parent) !== undefined);
    };
    
    function longProto() {
      var current = Object.create(null);
      current.i = 1;
      var previous, counter;
      for (var i = 0; i < 1000; i++) {
        previous = current;
        current = Object.create(previous);
        counter = counter + current.i;
      }
    
      return counter;
    }
    
    function longMap() {
      var current = new Map();
      current.i = 1;
      var previous, counter;
      for (var i = 0; i < 1000; i++) {
        previous = current;
        current = new Map(previous);
        counter = counter + current.get('i');
      }
      
      return counter;
    }
    
    function longCopy() {
      var current = {};
      current.i = 1;
      var previous, counter;
      for (var i = 0; i < 1000; i++) {
        previous = current;
        current = {i : previous.i};
        counter = counter + current.i;
      }
    
      return counter;
    }
    
    var longProtoCount = 0;
    var longCopyCount = 0;
    var longMapCount = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
prototype chain
longProtoCount = longProtoCount + longProto();
ready
homegrown map
longMapCount = longMapCount + longMap();
ready

Revisions

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

  • Revision 3: published by Forrest L Norvell on