Traditional OOP vs Derek's OOP Variant

JavaScript performance comparison

Test case created by Aadit M Shah

Info

Read the following StackOverflow question to know more.

Preparation code

<script>
function Shape() {
  this.x = 0;
  this.y = 0;
}

Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
}

Object.prototype.method = function(name, func) {
  if (typeof(this.constructor.prototype[name]) === 'undefined') {
    this.constructor.prototype[name] = func
  }
}

function DereksShape() {
  this.x = 0;
  this.y = 0;

  this.method('move', function(x, y) {
    this.x += x;
    this.y += y;
  })
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Traditional OOP
var shape = new Shape;
shape.move(2, 3);
pending…
Derek's OOP Variant
var shape = new DereksShape;
shape.move(2, 3);
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

Add a comment