The Power Of Getters - alwaysGetter

JavaScript performance comparison

Test case created by WebReflection

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var defineLazyAccessor = function() {
    var
      O = Object,
      defineProperty = O.defineProperty,
      // be sure no properties can be inherited
      // reused descriptor for prototypes
      dProto = O.create(null),
      // reused descriptor for properties
      dThis = O.create(null)
    ;
    // must be able to be removed
    dThis.configurable = true;
    return function defineLazyAccessor(
      proto, name, getNewValue, notEnumerable
    ) {
      dProto.enumerable = !notEnumerable;
      dProto.get = function () {
        dThis.enumerable = !notEnumerable;
        dThis.value = getNewValue.call(this);
        return defineProperty(this, name, dThis)[name];
      };
      defineProperty(proto, name, dProto);
    };
  }();
  
  function Element_alwaysGetter(){}
  function Element_optimized(){}
  
  // children
  defineLazyAccessor(
    Element_alwaysGetter.prototype,
    "children",
    function () {
      return [];
    }
  );
  defineLazyAccessor(
    Element_optimized.prototype,
    "children",
    function () {
      return [];
    }
  );
  
  // firstChild
  Object.defineProperty(
    Element_alwaysGetter.prototype,
    "firstChild", {
      enumerable: true,
      get: function () {
          return this.children[0];
      }
    }
  );
  defineLazyAccessor(
    Element_optimized.prototype,
    "firstChild",
    function () {
      return this.children[0];
    }
  );
  
  // lastChild
  Object.defineProperty(
    Element_alwaysGetter.prototype,
    "lastChild", {
      enumerable: true,
      get: function () {
          return this.children[
            this.children.length - 1
          ];
      }
    }
  );
  defineLazyAccessor(
    Element_optimized.prototype,
    "lastChild",
    function () {
      return this.children[
        this.children.length - 1
      ];
    }
  );
  
  // the method to appendChild
  Element_alwaysGetter.prototype.appendChild = function(el) {
    this.children.push(el);
    return el;
  };
  Element_optimized.prototype.appendChild = function(el) {
    delete this.firstChild;
    delete this.lastChild;
    this.children.push(el);
    return el;
  };
  
  var result;

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Element_alwaysGetter
var el = new Element_alwaysGetter, i = 0;
el.appendChild(0);
el.appendChild(1);
while (i++ < 1000) result = el.firstChild || el.lastChild;
pending…
Element_optimized
var el = new Element_optimized, i = 0;
el.appendChild(0);
el.appendChild(1);
while (i++ < 1000) result = el.firstChild || el.lastChild;
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.

0 Comments