Closure vs. property

JavaScript performance comparison

Revision 25 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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;
      };
  }
  var closureColor = new ClosureColor('green');
  
  
  function PropertyColor(name) {
      this._name = name;
      this.getName = function () {
          return this._name;
      };
      this.setName = function (n) {
          this._name = n;
      };
  }
  var propertyColor = new PropertyColor('green');
  
  
  function PrototypeColor(name) {
      this._name = name;
  }
  PrototypeColor.prototype.getName = function () {
      return this._name;
  };
  PrototypeColor.prototype.setName = function (n) {
      this._name = n;
  };
  var prototypeColor = new PrototypeColor('green');
  
  class ClassColor {
    constructor(name) {
      this._name = name
    }
    setName(name) {
      this._name = name
    }
    getName(name) {
      return this._name
    }
  }
  var classColor = new ClassColor("green")

};
</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
Class
var name = classColor.getName();
classColor.setName('blue');
pending…

Revisions

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

0 Comments