Prototype vs. this

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function T() {
  }
  
  T.prototype.myMethod = function(i) {
    if (i == 20000) {
      console.log('Hey T!');
    }
  };
  
  function Tt() {
    this.myMethod = function(i) {
      if (i == 20000) {
        console.log('Hey Tt!');
      }
    };
  }

};
</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
Using prototype
for (var i = 1; i <= 20000; i++) {
  var x = new T();
  x.myMethod(i);
}
pending…
Using this
for (var i = 1; i <= 20000; i++) {
  var x = new Tt();
  x.myMethod(i);
}
pending…

Compare results of other browsers

Revisions

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

0 Comments