Access object properties via closure vs. this

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

<script>
  var ObjClosure = function(count) {
    var _count = count;
    this.next = function() { return ++_count }
  }
  var ObjSelfClosure = function(count) {
    this._count = count
    self = this
    this.next = function() { return ++self._count }
  }
  var ObjThis = function(count) {
    this._count = count
    this.next = function() { return ++this._count }
  }
  var objClosure = new ObjClosure(0),
      objSelfClosure = new ObjSelfClosure(0),
      objThis = new ObjThis(0)
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Access properties via closure
objClosure.next()
pending…
Access properties via self closure
objSelfClosure.next()
pending…
Access properties via this
objThis.next()
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.

1 Comment

Sean Silva commented :

The self variable in objSelfClosure.next() is actually a global, not a closure variable, which makes the result inaccurate.