Prototype vs Constructor (v3)

Revision 3 of this benchmark created on


Description

Comparing pure prototype code vs constructor functions

Setup

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";
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype Style
var dog = Object.create(ProtoDog);
dog.name = "Scamp";
dog.getName();
dog.speak();
ready
Constructor
var dog = new CtorDog("Scamp");
dog.getName();
dog.speak();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.