Calling public methods accessing private vars (v12)

Revision 12 of this benchmark created on


Description

Checking the performance of calling public methods with private variables.

Preparation HTML

<script>
  function ConstructorPrivateProperties() {
    var privateProperty1 = 1,
        privateProperty2 = 2,
        privateProperty3 = 3,
        privateProperty4 = 4,
        privateProperty5 = 5,
        privateProperty6 = 6,
        privateProperty7 = 7,
        privateProperty8 = 8,
        privateProperty9 = 9,
        privateProperty10 = 10;

    this.publicMethod1 = function() {
      return privateProperty1 + privateProperty2 + privateProperty3 + privateProperty4 + privateProperty5 + privateProperty6 + privateProperty7 + privateProperty8 + privateProperty9 + privateProperty10;
    };
  }

  function PrototypePrivateProperties() {
    this._privateProperty1 = 1;
    this._privateProperty2 = 2;
    this._privateProperty3 = 3;
    this._privateProperty4 = 4;
    this._privateProperty5 = 5;
    this._privateProperty6 = 6;
    this._privateProperty7 = 7;
    this._privateProperty8 = 8;
    this._privateProperty9 = 9;
    this._privateProperty10 = 10;
  }

  PrototypePrivateProperties.prototype = {
    publicMethod1: function() {
      return this._privateProperty1 + this._privateProperty2 + this._privateProperty3 + this._privateProperty4 + this._privateProperty5 + this._privateProperty6 + this._privateProperty7 + this._privateProperty8 + this._privateProperty9 + this._privateProperty10;
    }
  };

  var instance1 = new ConstructorPrivateProperties();
  var instance2 = new PrototypePrivateProperties();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
call constructor
for (var i = 0; i < 5000; i++) {
  instance1.publicMethod1();
}
ready
call prototype
for (var i = 0; i < 5000; i++) {
  instance2.publicMethod1();
}
ready

Revisions

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