Object.observe polyfill (v8)

Revision 8 of this benchmark created by Misko Hevery on


Setup

function createWatch1(last) {
      var field = 'a' + Math.floor(Math.random()*999).toString(16);
      var obj = {};
      obj[field] = 1;
      return (last.next = {
        obj: obj,
        lastValue: 1,
        field: field,
        next: null
      });
    }
    function createWatch2(last) {
      var field = 'a' + Math.floor(Math.random()*999).toString(16);
      var obj = {};
      obj[field] = 1;
      return (last.next = {
        obj0: obj, lastValue0: 1, field0: field,
        obj1: obj, lastValue1: 1, field1: field,
        obj2: obj, lastValue2: 1, field2: field,
        obj3: obj, lastValue3: 1, field3: field,
        obj4: obj, lastValue4: 1, field4: field,
        obj5: obj, lastValue5: 1, field5: field,
        obj6: obj, lastValue6: 1, field6: field,
        obj7: obj, lastValue7: 1, field7: field,
        obj8: obj, lastValue8: 1, field8: field,
        obj9: obj, lastValue9: 1, field9: field,
        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);
    }
    dirtyCheck1 = function() {
      var current = head1;
      while(current) {
        if (current.obj[current.field] !== current.lastValue) {
           throw "Should not get here";
        }
        current = current.next;
      }
    };
    dirtyCheck2 = function() {
      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;
      }
    };
    
    ///////////////////////////////////////
    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 = {};
      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
dirtyCheck1()
ready
dirtyCheck2
dirtyCheck2()
ready
dirtyCheck3
for(var i=0, ii=objs.length; i<ii; i++) {
  if (objs[i][objFields[i]] !== lastValues[i]) throw 'x';
}
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