more private methods on prototype

JavaScript performance comparison

Revision 2 of this test case created

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
function createDoctor(name, specialty, mySallary) {
  return (function(name, specialty, sallary) {

    function Doctor() {};

    Doctor.prototype = (function() {
      return {
        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();
  }(name, specialty, mySallary));
}
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 method
function Doctor(myName, mySpecialty, mySallary) {

  this.getName = function() {
    return name;
  }

  this.setName = function(value) {
    name = value;
  }
  this.getSpecialty = function() {
    return specialty;
  }
  this.setSpecialty = function(value) {
    specialty = value;
  }
  this.getSallary = function() {
    return sallary;
  }
  this.setSallary = function(value) {
    sallary = value;
  }
}
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