more private methods on prototype

JavaScript performance comparison

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, mySallary) {

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

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

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

    return new Doctor(name, specialty, mySallary);


  }(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(name, specialty, sallary) {
                        this.name = name;
                        this.specialty = specialty;
                       
                  var sallary = sallary
                  this.getSallary = function() {
                      return sallary;
                      }
                  this.setSallary = function(value) {
                    sallary = value;
                  }
                }
                Doctor.prototype = (function() {
                  return {
                    // 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;
                    }
                  };

                }());

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