lazy getter

Benchmark created on


Preparation HTML

<script>
  var DATA = '{"pewpew": "pewpew", "abc": 157, "aoseuoq": "askdhqio", "a": false}';
  
  var MyObjectLazy = function (url, req, res, path, body) {
      this.res = res;           // Object
      this.req = req;           // Object
      this.body = body;         // String
      this.url = url;           // Object
      this.method = req.method; // String
      this.get = url.query;     // Object
      this.path = path || {};   // Object
      
      this.__defineGetter__("post", function () {
          var post = this.body ? JSON.parse(this.body) : {};
  
          delete this.post;
          this.post = post;
          return post;
      });
  };
  
  var MyObject = function (url, req, res, path, body) {
      this.res = res;           // Object
      this.req = req;           // Object
      this.body = body;         // String
      this.url = url;           // Object
      this.method = req.method; // String
      this.get = url.query;     // Object
      this.path = path || {};   // Object
      this.post = this.body ? JSON.parse(this.body) : {};
  };
  
  var MyObjectLazyProto = function (url, req, res, path, body) {
      this.res = res;           // Object
      this.req = req;           // Object
      this.body = body;         // String
      this.url = url;           // Object
      this.method = req.method; // String
      this.get = url.query;     // Object
      this.path = path || {};   // Object
  
      this._post;
  };
  
  MyObjectLazyProto.prototype.__defineGetter__("post", function () {
      if (this._post) return this._post;
      this._post = this.body ? JSON.parse(this.body) : {};
      return this._post;
  });
  
  var MyObjectLazyProtoDef = function (url, req, res, path, body) {
      this.res = res;           // Object
      this.req = req;           // Object
      this.body = body;         // String
      this.url = url;           // Object
      this.method = req.method; // String
      this.get = url.query;     // Object
      this.path = path || {};   // Object
  };
  
  MyObjectLazyProtoDef.prototype.__defineGetter__("post", function () {
      var data = this.body ? JSON.parse(this.body) : {};
      Object.defineProperty(this, "post", {value : data});
      return data;
  });
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
MyObject 50/50
var myObject1 = new MyObject({},{},{},{}, DATA);
myObject1.post;
var myObject2 = new MyObject({},{},{},{}, DATA);
ready
MyObjectLazy 50/50
var myObject1 = new MyObjectLazy({},{},{},{}, DATA);
myObject1.post;
var myObject2 = new MyObjectLazy({},{},{},{}, DATA);
ready
MyObjectLazyProto 50/50
var myObject1 = new MyObjectLazyProto({},{},{},{}, DATA);
myObject1.post;
var myObject2 = new MyObjectLazyProto({},{},{},{}, DATA);
ready
MyObject 100
var myObject1 = new MyObject({},{},{},{}, DATA);
myObject1.post;
ready
MyObjectLazy 100
var myObject1 = new MyObjectLazy({},{},{},{}, DATA);
myObject1.post;
ready
MyObjectLazyProto 100
var myObject1 = new MyObjectLazyProto({},{},{},{}, DATA);
myObject1.post;
ready
MyObjectLazyProtoDef 50/50
var myObject1 = new MyObjectLazyProtoDef({},{},{},{}, DATA);
myObject1.post;
var myObject2 = new MyObjectLazyProtoDef({},{},{},{}, DATA);
ready
MyObjectLazyProtoDef 100
var myObject1 = new MyObjectLazyProtoDef({},{},{},{}, DATA);
myObject1.post;
ready

Revisions

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