Prototype vs. this vs. Module (v24)

Revision 24 of this benchmark created by Digitale Welten on


Preparation HTML

<script>

CACHE_1 = {};
CACHE_2 = {};
CACHE_3 = {};
CACHE_4 = {};
CACHE_5 = new Array(100);

function T1(name, _cache){ this.name = name; this.cache = _cache; }
T1.prototype.getCache = function(i) { return this.cache[i]; };
T1.prototype.setCache = function(i, val) { this.cache[i] = val; return false; };

function C1(name) {
 this.name = name;
 this.keys = new Array(100);
 this.vals = new Array(100);
 this.len = 0;
};
C1.prototype.getCache = function(key) {
  var _this = this;
  var i, len = _this.len;
  for(i=0;i!==len;i+=1) if(_this.keys[i]===key) return _this.vals[i];
  return false;
};
C1.prototype.setCache = function(key, val) {
  var _this = this;
  var i, len = _this.len, found = false;
  for(i=0;i!==len;i+=1) if(_this.keys[i]===key) {
    found = true;
    break;
  }
  if(!found) {
    _this.len+=1;
    _this.keys[i] = key;
  }
  _this.vals[i] = val; 
  return true;
};

function C2(name, _cache) {
 this.name = name;
 this.cache = _cache;
 this.len = 0;
}
C2.prototype.getCache = function(key) {
  var i, cache = this.cache;
  for(i=0;i!==this.len;i+=1) { if(cache[i][0]===key) return cache[i][1]; }
  return false;
};
C2.prototype.setCache = function(key, val) {
  var _this = this;
  var i, len = _this.len, found = false;
  for(i=0;i!==len;i+=1) if(_this.cache[i][0]===key) {
    found = true;
    break;
  }
  _this.cache[i] = [key, val];
  if(!found) _this.len+=1;
  return true;
};

function C3(name) {
 this.name = name;
 this.keys = new Array(100);
 this.vals = new Array(100);
 this.len = 0;
};
C3.prototype.getCache = function(key) {
  var _this = this;
  if(_this.len) return _this.vals[_this.keys.indexOf(key)];
  return false;
};
C3.prototype.setCache = function(key, val) {
  var _this = this;
  var i, len = _this.len, found = false;
  for(i=0;i!==len;i+=1) if(_this.keys[i]===key) {
    found = true;
    break;
  }
  if(!found) {
    _this.len+=1;
    _this.keys[i] = key;
  }
  _this.vals[i] = val; 
  return false;
};

function T2(name, _cache) {
  this.name = name;
  this.cache = _cache;
  this.getCache = function(i) {
    return this.cache[i];
  };
  this.setCache = function(i, val) {
    this.cache[i] = val;
    return false;
  };
  //return this;
};

var T3 = function (_cache) {
  return {
    cache : _cache,
    getCache : function(i) {
      return this.cache[i];
    },
    setCache : function(i, val) {
      this.cache[i] = val;
      return false;
    }
  };
};

var T4 = function (_cache) {
  var $cache = _cache;
  return {
    cache : $cache,
    getCache : function(i) {
      return this.cache[i];
    },
    setCache : function(i, val) {
      this.cache[i] = val;
      return false;
    }
  };
};

var t1 = new T1('T1', CACHE_1);
var t2 = new T2('T2', CACHE_2);
var t3 = T3(CACHE_3);
var t4 = T4(CACHE_4);
var c1 = new C1('C1');
var c2 = new C2('C2', CACHE_5);
var c3 = new C3('C3');

</script>

Setup

//no definitions here!!!

Test runner

Ready to run.

Testing in
TestOps/sec
Set Using prototype
t1.setCache('test1_0', 'test');
t1.setCache('test1_1', 'test');
t1.setCache('test1_2', 'test');
t1.setCache('test1_3', 'test');
t1.setCache('test1_4', 'test');
t1.setCache('test1_5', 'test');
t1.setCache('test1_6', 'test');
t1.setCache('test1_7', 'test');
t1.setCache('test1_8', 'test');
t1.setCache('test1_9', 'test');
ready
Set Using this
t2.setCache('test2_0', 'test');
t2.setCache('test2_1', 'test');
t2.setCache('test2_2', 'test');
t2.setCache('test2_3', 'test');
t2.setCache('test2_4', 'test');
t2.setCache('test2_5', 'test');
t2.setCache('test2_6', 'test');
t2.setCache('test2_7', 'test');
t2.setCache('test2_8', 'test');
t2.setCache('test2_9', 'test');
ready
Set Using Module
t3.setCache('test3_0', 'test');
t3.setCache('test3_1', 'test');
t3.setCache('test3_2', 'test');
t3.setCache('test3_3', 'test');
t3.setCache('test3_4', 'test');
t3.setCache('test3_5', 'test');
t3.setCache('test3_6', 'test');
t3.setCache('test3_7', 'test');
t3.setCache('test3_8', 'test');
t3.setCache('test3_9', 'test');
ready
Set Using Module 2
t4.setCache('test4_0', 'test');
t4.setCache('test4_1', 'test');
t4.setCache('test4_2', 'test');
t4.setCache('test4_3', 'test');
t4.setCache('test4_4', 'test');
t4.setCache('test4_5', 'test');
t4.setCache('test4_6', 'test');
t4.setCache('test4_7', 'test');
t4.setCache('test4_8', 'test');
t4.setCache('test4_9', 'test');
ready
Set Using prototype + Emulated Assoziative Key Access with 2 seperate Arrays
c1.setCache('test5_0', 'test');
c1.setCache('test5_1', 'test');
c1.setCache('test5_2', 'test');
c1.setCache('test5_3', 'test');
c1.setCache('test5_4', 'test');
c1.setCache('test5_5', 'test');
c1.setCache('test5_6', 'test');
c1.setCache('test5_7', 'test');
c1.setCache('test5_8', 'test');
c1.setCache('test5_9', 'test');
ready
Set Using prototype + Emulated Assoziative Key Access with an 2D-Array
c2.setCache('test6_0', 'test');
c2.setCache('test6_1', 'test');
c2.setCache('test6_2', 'test');
c2.setCache('test6_3', 'test');
c2.setCache('test6_4', 'test');
c2.setCache('test6_5', 'test');
c2.setCache('test6_6', 'test');
c2.setCache('test6_7', 'test');
c2.setCache('test6_8', 'test');
c2.setCache('test6_9', 'test');
ready
Set Using prototype + Emulated Assoziative Key Access with 2 seperate Arrays + indexOf
c3.setCache('test7_0', 'test');
c3.setCache('test7_1', 'test');
c3.setCache('test7_2', 'test');
c3.setCache('test7_3', 'test');
c3.setCache('test7_4', 'test');
c3.setCache('test7_5', 'test');
c3.setCache('test7_6', 'test');
c3.setCache('test7_7', 'test');
c3.setCache('test7_8', 'test');
c3.setCache('test7_9', 'test');
ready
Get Using prototype
t1.getCache('test1_0');
t1.getCache('test1_1');
t1.getCache('test1_2');
t1.getCache('test1_3');
t1.getCache('test1_4');
t1.getCache('test1_5');
t1.getCache('test1_6');
t1.getCache('test1_7');
t1.getCache('test1_8');
t1.getCache('test1_9');
ready
Get Using this
t2.getCache('test2_0');
t2.getCache('test2_1');
t2.getCache('test2_2');
t2.getCache('test2_3');
t2.getCache('test2_4');
t2.getCache('test2_5');
t2.getCache('test2_6');
t2.getCache('test2_7');
t2.getCache('test2_8');
t2.getCache('test2_9');
ready
Get Using Module
t3.getCache('test3_0');
t3.getCache('test3_1');
t3.getCache('test3_2');
t3.getCache('test3_3');
t3.getCache('test3_4');
t3.getCache('test3_5');
t3.getCache('test3_6');
t3.getCache('test3_7');
t3.getCache('test3_8');
t3.getCache('test3_9');
ready
Get Using Module 2
t4.getCache('test4_0');
t4.getCache('test4_1');
t4.getCache('test4_2');
t4.getCache('test4_3');
t4.getCache('test4_4');
t4.getCache('test4_5');
t4.getCache('test4_6');
t4.getCache('test4_7');
t4.getCache('test4_8');
t4.getCache('test4_9');
ready
Get Using prototype + Emulated Assoziative Key Access with 2 seperate Arrays
c1.getCache('test5_0');
c1.getCache('test5_1');
c1.getCache('test5_2');
c1.getCache('test5_3');
c1.getCache('test5_4');
c1.getCache('test5_5');
c1.getCache('test5_6');
c1.getCache('test5_7');
c1.getCache('test5_8');
c1.getCache('test5_9');
ready
Get Using prototype + Emulated Assoziative Key Access with an 2D-Array
c2.getCache('test6_0');
c2.getCache('test6_1');
c2.getCache('test6_2');
c2.getCache('test6_3');
c2.getCache('test6_4');
c2.getCache('test6_5');
c2.getCache('test6_6');
c2.getCache('test6_7');
c2.getCache('test6_8');
c2.getCache('test6_9');
ready
Get Using prototype + Emulated Assoziative Key Access with 2 seperate Arrays + indexOf
c3.getCache('test7_0');
c3.getCache('test7_1');
c3.getCache('test7_2');
c3.getCache('test7_3');
c3.getCache('test7_4');
c3.getCache('test7_5');
c3.getCache('test7_6');
c3.getCache('test7_7');
c3.getCache('test7_8');
c3.getCache('test7_9');
ready

Revisions

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