Dynamic getter with private variable

JavaScript performance comparison

Test case created

Info

Create dynamic getter/setter around a concept that let you hide the variable so it's only accessible via getter/setter

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Accessible() {
      var accessory, _i, _len, _ref;
      _ref = ["name"];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        accessory = _ref[_i];
        this[accessory] = function() {
          var fn, value,
            _this = this;
          value = void 0;
          fn = function(v) {
            if (v === void 0) {
              return value;
            }
            return value = v;
          };
          this[accessory] = fn;
          return this[accessory]();
        };
        return this[accessory]();
      }
    };
   
    accessible = new Accessible();
    accessible.name("Test");
   
    function Normal() {
    };
   
    normal = new Normal();
    normal.name = function() {
        return "Test";
    };
   
    function Prototyped() {
      Prototyped.prototype.name = function() {
        return "Test";
      };
    };
   
    prototyped = new Prototyped();
};
</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
Normal setter
normal.name();
pending…
Prototype
prototyped.name();
pending…
Dynamic
accessible.name();
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