Prototype Class.create vs pure JS function with class definition time + instance creation time

JavaScript performance comparison

Test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var result = '';
  
  function extend(target, source) {
    for (var i in source) {
      target[i] = source[i]
    }
  }

};
</script>

Preparation code output

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></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
Pure JS
function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
}

Animal.prototype.speak = function() {
  result = (this.name + " says: " + this.sound + "!");
}

var cat = new Animal('Kitty', 'Meow');
cat.speak();
pending…
Pure + underscore extend prototype
function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
}
_.extend(Animal.prototype, {
  speak: function() {
    result = (this.name + " says: " + this.sound + "!");
  }
})

var cat = new Animal('Kitty', 'Meow');
cat.speak();
pending…
Pure JS no prototype
function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
  this.speak = function() {
    result = (this.name + " says: " + this.sound + "!");
  }
}

var cat = new Animal('Kitty', 'Meow');
cat.speak();
pending…
Pure JS prototype with simple extend function
function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
}
var protoMethods = {
  speak: function() {
    result = (this.name + " says: " + this.sound + "!");
  }
}
extend(Animal.prototype, protoMethods)


var cat = new Animal('Kitty', 'Meow');
cat.speak();
pending…

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

Compare results of other browsers

0 Comments