Prototype vs Constructor

JavaScript performance comparison

Revision 4 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.getName = function() {
    return this.name;
  };
  
  ProtoDog = Object.create(ProtoAnimal);
  ProtoDog.speak = function() {
    return "woof";
  };
  
  function ImutAnimal(name){
    var _name = name;
     
    var getName = function() {
      return _name;
    };
  
    return Object.freeze({
      getName: getName
    });
  }
  
  function ImutDog(name) {
     var _animal = ImutAnimal(name);
      
     var speak = function() {
       return 'woof';
     };
  
     return Object.freeze({
       getName: _animal.getName,
       speak: speak
     });
  }
  

};
</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 pdog = Object.create(ProtoDog);
pdog.name = "Scamp";
pdog.getName();
pdog.speak();
pending…
Constructor
var cdog = new CtorDog("Scamp");
cdog.getName();
cdog.speak();
pending…
Immutable Object
var idog = ImutDog("Scamp");
idog.getName();
idog.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