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

Revision 8 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="http://digg.googlecode.com/files/Class-0.0.2.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js">
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.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
jQuery Class.create
var Animal = Class.create({
  init: function(name, sound) {
    this.name = name;
    this.sound = sound;
  },
  speak: function() {
    result = (this.name + " says: " + this.sound + "!");
  }
});

var cat = new Animal('Kitty', 'Meow');
cat.speak();
ready
Prototype Class.create
var Animal = Class.create({
  initialize: function(name, sound) {
    this.name = name;
    this.sound = sound;
  },

  speak: function() {
    alert(this.name + " says: " + this.sound + "!");
  }
});
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 + 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();
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 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();
ready
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();
ready
Object.create
function Animal(name, sound) {
  this.name = name;
  this.sound = sound;
}

Animal.prototype = Object.create({
  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.