Closure vs. property

JavaScript performance comparison

Revision 12 of this test case created

Preparation code

<script>
function ClosureColor(name) {
    // Have no choice but to add these methods to the instance
    this.getName = function () {
        return name;
    };
    this.setName = function (n) {
        name = n;
    };
}


function ClosureReturnColor(name) {
    // Return object instead of attaching to Function
    return {
      getName: function () {
        return name;
      },
      setName: function (n) {
        name = n;
      }
    }
}

function PropertyColor(name) {
    this._name = name;
    this.getName = function () {
        return this._name;
    };
    this.setName = function (n) {
        this._name = n;
    };
}



function PrototypeColor(name) {
    this._name = name;
}
PrototypeColor.prototype.getName = function () {
    return this._name;
};
PrototypeColor.prototype.setName = function (n) {
    this._name = n;
};


function GetterSetterColor(name){
    var value = name;
   
    this.__defineGetter__("name", function(){
        return value;
    });
   
    this.__defineSetter__("name", function(val){
        value = val;
    });
}

function ProtoGetterSetterColor(name){
    this._name = name;
}

ProtoGetterSetterColor.prototype = {
  get name() {
    return this._name;
  },
  set name(name) {
    return this._name = name;
  }
}

function ObjectColor(name) {
    return {_name: name};
}

ObjectColor.getName = function(object) {
  return object._name;
}

ObjectColor.setName = function(object, name) {
   object._name = name;
}

function ObjectColor2(name) {
    this._name = name;
}

ObjectColor2.getName = function(object) {
  return object._name;
}

ObjectColor2.setName = function(object, name) {
   object._name = name;
}

function ObjectColor3(name) {
    this._name = name;
}

function ObjectColor3_getName(object) {
  return object._name;
}

function ObjectColor3_setName(object, name) {
   object._name = name;
}

</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
Closure
var closureColor = new ClosureColor('green');
var name = closureColor.getName();
closureColor.setName('blue');
pending…
Property
var propertyColor = new PropertyColor('green');
var name = propertyColor.getName();
propertyColor.setName('blue');
pending…
Prototype
var prototypeColor = new PrototypeColor('green');
var name = prototypeColor.getName();
prototypeColor.setName('blue');
pending…
Closure Return
var closureReturnColor = new ClosureReturnColor('green');
var name = closureReturnColor.getName();
closureReturnColor.setName('blue');
pending…
Getter Setter
var getterSetter = new GetterSetterColor('green');
var name = getterSetter.name;
getterSetter.name = 'blue';
pending…
Prototype Getter Setter
var protoGetterSetter = new ProtoGetterSetterColor('green');
var name = protoGetterSetter.name;
protoGetterSetter.name = 'blue';
pending…
Object
var objectColor = ObjectColor('green');
var name = ObjectColor.getName(objectColor);
ObjectColor.setName(objectColor, 'blue')
pending…
Object2
var objectColor2 = new ObjectColor2('green');
var name = ObjectColor2.getName(objectColor2);
ObjectColor2.setName(objectColor2, 'blue')
pending…
Object3
var objectColor3 = new ObjectColor3('green');
var name = ObjectColor3_getName(objectColor3);
ObjectColor3_setName(objectColor3, 'blue')
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