even more private methods on prototype

JavaScript performance comparison

Revision 2 of this test case created by vamp

Preparation code

<script type="text/javascript">
function createDoctor(name, specialty, sallary) {
    function Doctor() {};

    Doctor.prototype = {
        setSpecialty: function(value) {
          specialty = value;
        },
        getSpecialty: function() {
          return specialty;
        },
        setName: function(value) {
          name = value;
        },
        getName: function() {
          return name;
        },
        // methods to set and get outer private variables
        getSallary: function() {
          return sallary;
        },
        setSallary: function(value) {
          sallary = value;
        }
    };

    return new Doctor();
}



function Doctor(name, specialty, sallary) {
  this.name = name;
  this.specialty = specialty;

  var sValue = sallary;
  this.getSallary = function() {
      return sValue;
  }
     
  this.setSallary = function(value) {
    sValue = value;
  }
}
Doctor.prototype = {
    // public attributes and methods
    setName: function(name) {
      this.name = name;
    },
    getName: function() {
      return this.name;
    },
    setSpecialty: function(value) {
      this.specialty = value;
    },
    getSpecialty: function() {
      return this.specialty;
    }
  };


</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
my method


var manos = createDoctor("Manos", "Gynae", 10000);
manos.setSallary(300000);
manos.getName() + " " + manos.getSallary();

var panos = createDoctor("Pano", "xxx", 0);
panos.setName("Panos");
panos.setSpecialty("Mathematician");
panos.setSallary(10);
panos.getName() + " " + panos.getSallary() + " " + panos.getSpecialty();
manos.getName() + " " + manos.getSallary();
pending…
Crockford's metnod


var manos = new Doctor("Manos", "Gynae", 10000);
manos.setSallary(300000);
manos.getName() + " " + manos.getSallary();

var panos = new Doctor("Pano", "xxx", 0);
panos.setName("Panos");
panos.setSpecialty("Mathematician");
panos.setSallary(10);
panos.getName() + " " + panos.getSallary() + " " + panos.getSpecialty();
manos.getName() + " " + manos.getSallary();
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