private methods on prototype

JavaScript performance comparison

Revision 2 of this test case created

Info

Can we put a private method in prototype property? Which method is faster mine or the Crockfords?

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 Doctor(name, specialty, sallary) {

  // var aString = "private variable"; // private variables
  // private variables: they will persist
  var name = name;
  var specialty = specialty;
  var sallary = sallary;

  function ProxyDoctor() {};

  ProxyDoctor.prototype = (function() {
    return {
      setSpecialty: function(value) {
        specialty = value;
      },
      getSpecialty: function() {
        return specialty;
      },
      setName: function(name) {
        name = name;
      },
      getName: function() {
        return name;
      },
      // methods to set and get outer private variables
      getSallary: function() {
        return sallary;
      },
      setSallary: function(value) {
        sallary = value;
      }
    };
  }());
  proxy = new ProxyDoctor(name, specialty);
  return proxy;
} // end of Doctor Constructor function
var manos = new Doctor("Manos", "Gynae", 10000);
manos.setSallary(300000);
manos.getName() + " " + manos.getSallary();

var panos = new Doctor("Manos", "Gynae", 10000);
panos.setName("Panos");
panos.setSpecialty("Mathematician");
panos.setSallary(500000);
panos.getName() + " " + panos.getSpecialty() + " " + panos.getSallary();
manos.getName() + " " + manos.getSallary();
pending…
Crockford's method
function Doctor(myName, mySpecialty, mySallary) {
                  var name = myName;
                  var specialty = mySpecialty;
                  var sallary = 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("Manos", "Gynae", 10000);
panos.setName("Panos");
panos.setSpecialty("Mathematician");
panos.setSallary(500000);
panos.getName() + " " + panos.getSpecialty() + " " + panos.getSallary();
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