Prototype vs. this (v54)

Revision 54 of this benchmark created on


Description

Uses fake or real private values to boot.

Setup

function T1(val) {this._fake = val;}
    T1.prototype.myMethod = function() {return this._fake;};
    
    function T2(val) {
      this._fake = val;
      // useless reasign
      T2.prototype.myMethod = function() {return this._fake;};
    }
    
    function T3(val) {
     this.myMethod = function() {return val;};
    }
    
    
    function T4(val) {
     var real = val;
     this.myMethod = function() {return real;};
    }
    
    
    function T5(val) {
     var real = val;
     // don't use capture
     this.myMethod = function() {return;};
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Using prototype
var t = new T1("test text");
var x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
ready
Using this
var t = new T2("test text");
var x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
ready
Test 3
var t = new T3("test text");
var x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
ready
Test 4
var t = new T4("test text");
var x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
ready
Test 5
var t = new T5("test text");
var x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
x = t.myMethod();
ready

Revisions

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