Object.observe polyfill (v10)

Revision 10 of this benchmark created by Misko Hevery on


Setup

function rndFieldName() {
      return 'a' + Math.floor(Math.random()*999).toString(16);
    }
    
    function createWatch1(last) {
      var field = rndFieldName();
      var obj = {};
      obj[field] = 1;
      return (last.next = {
        obj: obj,
        lastValue: 1,
        field: field,
        next: null
      });
    }
    function createWatch2(last) {
      var field = rndFieldName();
      var getter = new Function('s', 'return s.'+field);
      var obj = {};
      obj[field] = 1;
      return (last.next = {
        obj0: obj, lastValue0: 1, field0: field, getter0:getter,
        obj1: obj, lastValue1: 1, field1: field, getter1:getter,
        obj2: obj, lastValue2: 1, field2: field, getter2:getter,
        obj3: obj, lastValue3: 1, field3: field, getter3:getter,
        obj4: obj, lastValue4: 1, field4: field, getter4:getter,
        obj5: obj, lastValue5: 1, field5: field, getter5:getter,
        obj6: obj, lastValue6: 1, field6: field, getter6:getter,
        obj7: obj, lastValue7: 1, field7: field, getter7:getter,
        obj8: obj, lastValue8: 1, field8: field, getter8:getter,
        obj9: obj, lastValue9: 1, field9: field, getter9:getter,
        next: null
      });
    }
    var head1 = createWatch1({});
    var current = head1;
    for(var i = 1; i < 10000; i++) {
      current = createWatch1(current);
    }
    
    var head2 = createWatch2({});
    var current = head2;
    for(var i = 1; i < 1000; i++) {
      current = createWatch2(current);
    }
    
    ///////////////////////////////////////
    function Watch1(previous) {
      if (previous) previous.next = this;
      this.field = rndFieldName();
      this.obj = {};
      this.lastValue = 1;
      this.getter = new Function('c', 'return c.' + this.field);
      this.obj[this.field] = this.lastValue;
      this.check = Watch1.prototype.check;
    }
    
    Watch1.prototype.check = function() {
      if (this.obj[this.field] !== this.lastValue) throw "DIRTY";
      return this.next;
    }
    
    var head3 = new Watch1();
    var current = head3;
    for(var i = 1; i < 10000; i++) {
      current = new Watch1(current);
    }
    
    ///////////////////////////////////////
    var size = 10000;
    var objs = new Array(size);
    var objFields = new Array(size);
    var lastValues = new Array(size);
    for(var i = 0; i < size; i++) {
      var obj = objs[i] = {};
      objFields[i] = 'a' + Math.floor(Math.random()*999).toString(16);
      lastValues[i] = obj[objFields[i]] = 1;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
dirtyCheck1
var current = head1;
while(current) {
  if (current.obj[current.field] !== current.lastValue) {
     throw "DIRTY";
  }
  current = current.next;
}
 
ready
dirtyCheck2
var c = head2;
while(c) {
  if (c.obj0[c.field0] !== c.lastValue0) throw "X";
  if (c.obj1[c.field1] !== c.lastValue1) throw "X";
  if (c.obj2[c.field2] !== c.lastValue2) throw "X";
  if (c.obj3[c.field3] !== c.lastValue3) throw "X";
  if (c.obj4[c.field4] !== c.lastValue4) throw "X";
  if (c.obj5[c.field5] !== c.lastValue5) throw "X";
  if (c.obj6[c.field6] !== c.lastValue6) throw "X";
  if (c.obj7[c.field7] !== c.lastValue7) throw "X";
  if (c.obj8[c.field8] !== c.lastValue8) throw "X";
  if (c.obj9[c.field9] !== c.lastValue9) throw "X";
  c = c.next;
}
 
ready
dirtyCheck3
for(var i=0, ii=objs.length; i<ii; i++) {
  if (objs[i][objFields[i]] !== lastValues[i]) throw 'x';
}
ready
for(var i=0, ii=objs.length; i<ii; i=i+10) {
  if (objs[i][objFields[i]] !== lastValues[i]) throw 'x';
  if (objs[i+1][objFields[i+1]] !== lastValues[i+1]) throw 'x';
  if (objs[i+2][objFields[i+2]] !== lastValues[i+2]) throw 'x';
  if (objs[i+3][objFields[i+3]] !== lastValues[i+3]) throw 'x';
  if (objs[i+4][objFields[i+4]] !== lastValues[i+4]) throw 'x';
  if (objs[i+5][objFields[i+5]] !== lastValues[i+5]) throw 'x';
  if (objs[i+6][objFields[i+6]] !== lastValues[i+6]) throw 'x';
  if (objs[i+7][objFields[i+7]] !== lastValues[i+7]) throw 'x';
  if (objs[i+8][objFields[i+8]] !== lastValues[i+8]) throw 'x';
  if (objs[i+9][objFields[i+9]] !== lastValues[i+9]) throw 'x';
}
ready
function getter
var c = head2;
while(c) {
  if (c.getter0(c.obj0) !== c.lastValue0) throw "X";
  if (c.getter1(c.obj1) !== c.lastValue1) throw "X";
  if (c.getter2(c.obj2) !== c.lastValue2) throw "X";
  if (c.getter3(c.obj3) !== c.lastValue3) throw "X";
  if (c.getter4(c.obj4) !== c.lastValue4) throw "X";
  if (c.getter5(c.obj5) !== c.lastValue5) throw "X";
  if (c.getter6(c.obj6) !== c.lastValue6) throw "X";
  if (c.getter7(c.obj7) !== c.lastValue7) throw "X";
  if (c.getter8(c.obj8) !== c.lastValue8) throw "X";
  if (c.getter9(c.obj9) !== c.lastValue9) throw "X";
  c = c.next;
}
 
ready
Watch class
var current = head3;
while(current) {
  if (current.obj[current.field] !== current.lastValue) {
     throw "DIRTY";
  }
  current = current.next;
}
 
ready
Watch Class method
var current = head3;
while(current = current.check());
 
ready
Watch class getter
var current = head3;
while(current) {
  if (current.getter(current.obj) !== current.lastValue) throw "DIRTY";
  current = current.next;
}
 
ready
dirtyCheck1 if
var current = head1;
var value, lastValue;
while(current) {
  t1 = current.obj[current.field];
  t2 = current.lastValue
  if (t1 == null ? t2 != null : t1 !== t2) throw "DIRTY";
  current = current.next;
}
 
ready

Revisions

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

  • Revision 1: published by Misko Hevery on
  • Revision 2: published by Misko Hevery on
  • Revision 3: published by Misko Hevery on
  • Revision 4: published by Misko Hevery on
  • Revision 5: published by Misko Hevery on
  • Revision 6: published by Misko Hevery on
  • Revision 7: published by Misko Hevery on
  • Revision 8: published by Misko Hevery on
  • Revision 9: published by Misko Hevery on
  • Revision 10: published by Misko Hevery on
  • Revision 11: published by Misko Hevery on
  • Revision 12: published by Misko Hevery on
  • Revision 13: published by Misko Hevery on
  • Revision 14: published by Misko Hevery on