jQuery Class.create vs Prototype Class.create vs pure JS function with class definition time + instance creation time (v20)

Revision 20 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://digg.googlecode.com/files/Class-0.0.2.js"></script>
<script src="https://raw.github.com/lodash/lodash-compat/3.10.1/lodash.min.js"></script>

Setup

var result = '';
  
  function extend(target, source) {
    for (var i in source) {
      target[i] = source[i]
    }
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Pure JS + extend prototype with object
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();
ready
Pure JS + extend prototype with "for"
function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
}
var protoMethods = {
  speak: function() {
    result = (this.name + " says: " + this.sound + "!");
  }
}
for (var i in protoMethods) {
  Animal.prototype[i] = protoMethods[i]
}

var cat = new Animal('Kitty', 'Meow');
cat.speak();
ready
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();
ready
Pure JS + prototype object
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();
ready

Revisions

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