Prototype vs. this vs. Module vs. Arguments vs. Direct (v26)

Revision 26 of this benchmark created by Digitale Welten on


Preparation HTML

<script>

var CACHE_1 = {};
var CACHE_2 = {};
//var CACHE_3 = {};
var CACHE_4 = {};

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) { return this.cache[i] = val };

function T5(name){ this.name = name; }
T5.prototype.getCache = function(i) { return this[i] };
T5.prototype.setCache = function(i, val) { return this[i] = val };

function T6(name){
  this.name = name;
  this.getCache = function(i) { return this[i]; };
  this.setCache = function(i, val) { return this[i] = val; };
}

function T7(name){ this.name = name; }
T7.prototype.ptype = T7.prototype;
T7.prototype.getCache = function(i) { return this.ptype[i] };
T7.prototype.setCache = function(i, val) { return this.ptype[i] = val };

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 = {
  cache : {},
  getCache : function(i) {
    return this.cache[i];
  },
  setCache : function(i, val) {
    this.cache[i] = val;
    return false;
  }
};

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

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');
var t5 = new T5('T5');
var t6 = new T6('T6');
var t7 = new T7('T7');

//function setHelper(c, k, v){ c[k]=v };
//function getHelper(c, k){ return c[k] };

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
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');
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
Property (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');
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
Module
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');
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
Prototype + Emulated Assoziative Key Access via 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');
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
Prototype + Emulated Assoziative Key Access via 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');
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
Property Storage + Prototype Get/Set
t5.setCache('test6_0', 'test');
t5.setCache('test6_1', 'test');
t5.setCache('test6_2', 'test');
t5.setCache('test6_3', 'test');
t5.setCache('test6_4', 'test');
t5.setCache('test6_5', 'test');
t5.setCache('test6_6', 'test');
t5.setCache('test6_7', 'test');
t5.setCache('test6_8', 'test');
t5.setCache('test6_9', 'test');
t5.getCache('test6_0');
t5.getCache('test6_1');
t5.getCache('test6_2');
t5.getCache('test6_3');
t5.getCache('test6_4');
t5.getCache('test6_5');
t5.getCache('test6_6');
t5.getCache('test6_7');
t5.getCache('test6_8');
t5.getCache('test6_9');
ready
Property Storage + Property Get/Set
t6.setCache('test8_0', 'test');
t6.setCache('test8_1', 'test');
t6.setCache('test8_2', 'test');
t6.setCache('test8_3', 'test');
t6.setCache('test8_4', 'test');
t6.setCache('test8_5', 'test');
t6.setCache('test8_6', 'test');
t6.setCache('test8_7', 'test');
t6.setCache('test8_8', 'test');
t6.setCache('test8_9', 'test');
t6.getCache('test8_0');
t6.getCache('test8_1');
t6.getCache('test8_2');
t6.getCache('test8_3');
t6.getCache('test8_4');
t6.getCache('test8_5');
t6.getCache('test8_6');
t6.getCache('test8_7');
t6.getCache('test8_8');
t6.getCache('test8_9');
ready
Prototype Storage + Prototype Get/Set
t7.setCache('test9_0', 'test');
t7.setCache('test9_1', 'test');
t7.setCache('test9_2', 'test');
t7.setCache('test9_3', 'test');
t7.setCache('test9_4', 'test');
t7.setCache('test9_5', 'test');
t7.setCache('test9_6', 'test');
t7.setCache('test9_7', 'test');
t7.setCache('test9_8', 'test');
t7.setCache('test9_9', 'test');
t7.getCache('test9_0');
t7.getCache('test9_1');
t7.getCache('test9_2');
t7.getCache('test9_3');
t7.getCache('test9_4');
t7.getCache('test9_5');
t7.getCache('test9_6');
t7.getCache('test9_7');
t7.getCache('test9_8');
t7.getCache('test9_9');
ready
Object Literal Notation
t3.setCache('test10_0', 'test');
t3.setCache('test10_1', 'test');
t3.setCache('test10_2', 'test');
t3.setCache('test10_3', 'test');
t3.setCache('test10_4', 'test');
t3.setCache('test10_5', 'test');
t3.setCache('test10_6', 'test');
t3.setCache('test10_7', 'test');
t3.setCache('test10_8', 'test');
t3.setCache('test10_9', 'test');
t3.getCache('test10_0');
t3.getCache('test10_1');
t3.getCache('test10_2');
t3.getCache('test10_3');
t3.getCache('test10_4');
t3.getCache('test10_5');
t3.getCache('test10_6');
t3.getCache('test10_7');
t3.getCache('test10_8');
t3.getCache('test10_9');
ready

Revisions

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