Access object properties via closure vs. this

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

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

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
Access properties via self closure
Access properties via this

Compare results of other browsers


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 is actually a global, not a closure variable, which makes the result inaccurate.