Access object properties via closure vs. this (v7)

Revision 7 of this benchmark created on


Preparation HTML

<script>
  var ObjClosure = function(count) {
    var _count = count;
    this.next = function() { return ++_count; };
  };
  var ObjSelfClosure = function(count) {
    this._count = count;
    var self = this;
    this.next = function() { return ++self._count; };
  };
  var ObjThis = function(count) {
    this._count = count;
    this.next = function() { return ++this._count; };
  };
  var _count2 = 0;
  var ObjClosure2 = function(count) {
    _count2 = count;
    this.next = function() { return ++_count2; };
  };
  var _count3 = 0;
  var createObjClosure3 = function(count) {
    var dummy = {}; // interm scope content
    var ObjClosure3 = function(count) {
      this.getDummy = function() { return dummy; };
      _count3 = count;
      this.next = function() { return ++_count3; };
    };
    return new ObjClosure3(count);
  };
  var _count4 = 0;
  var createObjClosure4 = function(count) {
    var dummyA = {}; // interm scope content
    var innerCreateObjClosure4 = function(count) {
      var dummyB = {}; // interm scope content
      var ObjClosure4 = function(count) {
        this.getDummyA = function() { return dummyA; };
        this.getDummyB = function() { return dummyB; };
        _count4 = count;
        this.next = function() { return ++_count4; };
      };
      return new ObjClosure4(count);
    };
    return innerCreateObjClosure4(count);
  };
  var objClosure = new ObjClosure(0);
  var objSelfClosure = new ObjSelfClosure(0);
  var objThis = new ObjThis(0);
  var objThisNextBoundToContext = objThis.next.bind(objThis);
  var objClosure2 = new ObjClosure2(0);
  var objClosure3 = createObjClosure3(0);
  var objClosure4 = createObjClosure4(0);
  var objThisNextBoundManually = (function(fn, context) {
   return function() {
     return fn.apply(context, arguments);
   };
})(objThis.next, objThis);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Access properties via closure
objClosure.next()
ready
Access properties via self closure
objSelfClosure.next()
ready
Access properties via this
objThis.next()
ready
Access properties via second order closure
objClosure2.next()
ready
Access properties via third order closure
objClosure3.next()
ready
Access properties via fourth order closure
objClosure4.next()
ready
objThis.next.bind(objThis)
objThisNextBoundToContext();
ready
objThisNextBoundManually
objThisNextBoundManually();
ready

Revisions

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