Getter/setter emulation performance study

JavaScript performance comparison

Revision 2 of this test case created

Info

Sorry, this is unlikely to be informative unless you are working on compilers targeting JavaScript

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // boxes with unconditional getters/setters
    function BoxGSU(v, t) {
      this.v = v;
    }
   
    BoxGSU.prototype.get = function () {
      return this.v;
    };
   
    BoxGSU.prototype.set = function (v) {
      return this.v = v;
    };
   
    function getGSU(b) {
      return b.get();
    }
   
    function setGSU(b, v) {
      b.set(v);
    }
   
    // boxes with conditional getters/setters
    function BoxGSC(v, t) {
      this.v = v;
      this.t = t;
    }
   
    function BoxGSC2(v, t) {
      this.v = v;
      this.t = t;
      this.get = null;
      this.set = null;
    }
   
    function BoxGSC3(v, t) {
      this.v = v;
      this.t = t;
      this.get = function() {};
      this.set = function() {};
    }
   
    function getGSC(b) {
      if (b.t) return b.get();
      else return b.v;
    }
   
    function setGSC(b, v) {
      if (b.t) b.set(v);
      else b.v = v;
    }
   
    // constructs an object with fields with no getters/setters
    var C0 = function (boxC) {
        this.a0 = new boxC(0, 0);
        this.a1 = new boxC(1, 0);
        this.a2 = new boxC(2, 0);
        this.a3 = new boxC(3, 0);
        this.a4 = new boxC(4, 0);
        this.a5 = new boxC(5, 0);
        this.a6 = new boxC(6, 0);
        this.a7 = new boxC(7, 0);
        this.a8 = new boxC(8, 0);
        this.a9 = new boxC(9, 0);
        this.a10= new boxC(10, 0);
        this.a11= new boxC(11, 0);
        this.a12= new boxC(12, 0);
        this.a13= new boxC(13, 0);
        this.a14= new boxC(14, 0);
        this.a15= new boxC(15, 0);
        this.a16= new boxC(16, 0);
        this.a17= new boxC(17, 0);
        this.a18= new boxC(18, 0);
        this.a19= new boxC(19, 0);
        this.a20= new boxC(20, 0);
    };
   
    // object with fields with 5% getters/setters
    var C5 = function (boxC) {
        this.a0 = new boxC(0, 0);
        this.a1 = new boxC(1, 0);
        this.a2 = new boxC(2, 0);
        this.a3 = new boxC(3, 0);
        this.a4 = new boxC(4, 0);
        this.a5 = new boxC(5, 0);
        this.a6 = new boxC(6, 0);
        this.a7 = new boxC(7, 0);
        this.a8 = new boxC(8, 0);
        this.a9 = new boxC(9, 0);
        this.a10= new boxC(10, 0);
        this.a11= new boxC(11, 0);
        this.a12= new boxC(12, 0);
        this.a13= new boxC(13, 0);
        this.a14= new boxC(14, 0);
        this.a15= new boxC(15, 0);
        this.a16= new boxC(16, 0);
        this.a17= new boxC(17, 0);
        this.a18= new boxC(18, 0);
        this.a19= new boxC(19, 0);
        this.a20= new boxC(20, 1);
        this.a20.get = function () {
          return 3*this.v;
        };
        this.a20.set = function (v) {
          this.v -= v;
        };
    };
   
    // object with fields with 10% getters/setters
    var C10 = function (boxC) {
        this.a0 = new boxC(0, 0);
        this.a1 = new boxC(1, 0);
        this.a2 = new boxC(2, 0);
        this.a3 = new boxC(3, 0);
        this.a4 = new boxC(4, 0);
        this.a5 = new boxC(5, 0);
        this.a6 = new boxC(6, 0);
        this.a7 = new boxC(7, 0);
        this.a8 = new boxC(8, 0);
        this.a9 = new boxC(9, 0);
        this.a10= new boxC(10, 0);
        this.a11= new boxC(11, 0);
        this.a12= new boxC(12, 0);
        this.a13= new boxC(13, 0);
        this.a14= new boxC(14, 0);
        this.a15= new boxC(15, 0);
        this.a16= new boxC(16, 0);
        this.a17= new boxC(17, 0);
        this.a18= new boxC(18, 0);
        this.a19= new boxC(19, 1);
        this.a19.get = function () {
          return 3*this.v;
        };
        this.a19.set = function (v) {
          this.v -= v;
        };
        this.a20= new boxC(20, 1);
        this.a20.get = function () {
          return 3*this.v;
        };
        this.a20.set = function (v) {
          this.v -= v;
        };
    };
   
    // object with fields with 50% getters/setters
    var C50 = function (boxC) {
        this.a0 = new boxC(0, 0);
        this.a1 = new boxC(0, 0);
        this.a2 = new boxC(2, 0);
        this.a3 = new boxC(3, 0);
        this.a4 = new boxC(4, 0);
        this.a5 = new boxC(5, 0);
        this.a6 = new boxC(6, 0);
        this.a7 = new boxC(7, 0);
        this.a8 = new boxC(8, 0);
        this.a9 = new boxC(9, 0);
        this.a10= new boxC(10, 1);
        this.a11= new boxC(11, 1);
        this.a12= new boxC(12, 1);
        this.a13= new boxC(13, 1);
        this.a14= new boxC(14, 1);
        this.a15= new boxC(15, 1);
        this.a16= new boxC(16, 1);
        this.a17= new boxC(17, 1);
        this.a18= new boxC(18, 1);
        this.a19= new boxC(19, 1);
        this.a20= new boxC(20, 1);
    };
   
    //object with 0% getters, using Unconditional boxes
    var o0u = new C0(BoxGSU);
    //object with 5% getters, using Unconditional boxes
    var o5u = new C5(BoxGSU);
    //object with 10% getters, using Unconditional boxes
    var o10u = new C10(BoxGSU);
   
    //object with 0% getters, using Conditional boxes
    var o0c = new C0(BoxGSC);
    //object with 5% getters, using Conditional boxes
    var o5c = new C5(BoxGSC);
    //object with 10% getters, using Conditional boxes
    var o10c = new C10(BoxGSC);
   
    //object with 0% getters, using Conditional boxes
    var o0c2 = new C0(BoxGSC2);
    //object with 5% getters, using Conditional boxes
    var o5c2 = new C5(BoxGSC2);
    //object with 10% getters, using Conditional boxes
    var o10c2 = new C10(BoxGSC2);
   
    //object with 0% getters, using Conditional boxes
    var o0c3 = new C0(BoxGSC3);
    //object with 5% getters, using Conditional boxes
    var o5c3 = new C5(BoxGSC3);
    //object with 10% getters, using Conditional boxes
    var o10c3 = new C10(BoxGSC3);
   
   
    function assertTrue(b) {if (!b) alert("assertion failure");}
    function assertFalse(b) {if (b) alert("assertion failure");}
    function update(o, g, s) {
      s(o.a1, g(o.a1)*2);
      s(o.a2, g(o.a2)*2);
      s(o.a3, g(o.a3)*2);
      s(o.a4, g(o.a4)*2);
      s(o.a5, g(o.a5)*2);
      s(o.a6, g(o.a6)*2);
      s(o.a7, g(o.a7)*2);
      s(o.a8, g(o.a8)*2);
      s(o.a9, g(o.a9)*2);
      s(o.a10, g(o.a10)*2);
      s(o.a11, g(o.a11)*2);
      s(o.a12, g(o.a12)*2);
      s(o.a13, g(o.a13)*2);
      s(o.a14, g(o.a14)*2);
      s(o.a15, g(o.a15)*2);
      s(o.a16, g(o.a16)*2);
      s(o.a17, g(o.a17)*2);
      s(o.a18, g(o.a18)*2);
      s(o.a19, g(o.a19)*2);
      s(o.a20, g(o.a20)*2);
    }
   
    function verify0(o, g) {
      return (g(o.a1) == 2 && g(o.a2) == 4 && g(o.a3) == 6 && g(o.a4) == 8 &&
          g(o.a5) == 10 && g(o.a6) == 12 && g(o.a7) == 14 && g(o.a8) == 16 &&
          g(o.a9) == 18 && g(o.a10) == 20 && g(o.a11) == 22 && g(o.a12) == 24 &&
          g(o.a13) == 26 && g(o.a14) == 28 && g(o.a15) == 30 && g(o.a16) == 32 && g(o.a17) == 34 &&
          g(o.a18) == 36 && g(o.a19) == 38 && g(o.a20) == 40);
    }
   
    function verify5(o, g) {
      return (g(o.a1) == 2 && g(o.a2) == 4 && g(o.a3) == 6 && g(o.a4) == 8 &&
          g(o.a5) == 10 && g(o.a6) == 12 && g(o.a7) == 14 && g(o.a8) == 16 &&
          g(o.a9) == 18 && g(o.a10) == 20 && g(o.a11) == 22 && g(o.a12) == 24 &&
          g(o.a13) == 26 && g(o.a14) == 28 && g(o.a15) == 30 && g(o.a16) == 32 && g(o.a17) == 34 &&
          g(o.a18) == 36 && g(o.a19) == 38 && g(o.a20) == -300);
    }
   
    function verify10(o, g) {
      return (g(o.a1) == 2 && g(o.a2) == 4 && g(o.a3) == 6 && g(o.a4) == 8 &&
          g(o.a5) == 10 && g(o.a6) == 12 && g(o.a7) == 14 && g(o.a8) == 16 &&
          g(o.a9) == 18 && g(o.a10) == 20 && g(o.a11) == 22 && g(o.a12) == 24 &&
          g(o.a13) == 26 && g(o.a14) == 28 && g(o.a15) == 30 && g(o.a16) == 32 && g(o.a17) == 34 &&
          g(o.a18) == 36 && g(o.a19) == -285 && g(o.a20) == -300);
    }
};

Benchmark.prototype.teardown = function() {
    o0u = o5u = o10u = o0c = o5c = o10c = null;
};
</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
Unconditional getters/setters and 0% getters/setters
update(o0u, getGSU, setGSU);
assertTrue(verify0(o0u, getGSU));
pending…
Unconditional getters/setters and 5% getters/setters
update(o5u, getGSU, setGSU);
assertTrue(verify5(o5u, getGSU));
pending…
Unconditional getters/setters and 10% getters/setters
update(o10u, getGSU, setGSU);
assertTrue(verify10(o10u, getGSU));
pending…
Conditional getters/setters and 0% getters/setters
update(o0c, getGSC, setGSC);
assertTrue(verify0(o0c, getGSC));
pending…
Conditional getters/setters and 5% getters/setters
update(o5c, getGSC, setGSC);
assertTrue(verify5(o5c, getGSC));
pending…
Conditional getters/setters and 10% getters/setters
update(o10c, getGSC, setGSC);
assertTrue(verify10(o10c, getGSC));
pending…
Conditional (ver 2) getters/setters and 0% getters/setters
update(o0c2, getGSC, setGSC);
assertTrue(verify0(o0c2, getGSC));
pending…
Conditional (ver 2) getters/setters and 5% getters/setters
update(o5c2, getGSC, setGSC);
assertTrue(verify5(o5c2, getGSC));
pending…
Conditional (ver 2) getters/setters and 10% getters/setters
update(o10c2, getGSC, setGSC);
assertTrue(verify10(o10c2, getGSC));
pending…
Conditional (ver 3) getters/setters and 0% getters/setters
update(o0c3, getGSC, setGSC);
assertTrue(verify0(o0c3, getGSC));
pending…
Conditional (ver 3) getters/setters and 5% getters/setters
update(o5c3, getGSC, setGSC);
assertTrue(verify5(o5c3, getGSC));
pending…
Conditional (ver 3) getters/setters and 10% getters/setters
update(o10c3, getGSC, setGSC);
assertTrue(verify10(o10c3, getGSC));
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. Here’s a list of current revisions for this page:

0 comments

Add a comment