Prototype vs Constructor

JavaScript performance comparison

Revision 5 of this test case created by Class creation options

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function CtorAnimal(name) {
    this.name = name;
  }
  CtorAnimal.prototype.getName = function() {
    return this.name;
  };
  
  function CtorDog(name) {
    CtorAnimal.call(this, name);
  }
  CtorDog.prototype = Object.create(CtorAnimal.prototype);
  CtorDog.prototype.speak = function() {
    return "woof";
  };
  
  ProtoAnimal = Object.create(Object);
  ProtoAnimal.name = "";
  ProtoAnimal.prototype.getName = function() {
    return this.name;
  };
  
  ProtoDog = Object.create(ProtoAnimal.prototype);
  ProtoDog.prototype.speak = function() {
    return "woof";
  };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Prototype Style
var dog = Object.create(ProtoDog);
dog.name = "Scamp";
dog.getName();
dog.speak();
pending…
Constructor
var dog = new CtorDog("Scamp");
dog.getName();
dog.speak();
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.

0 Comments