Setter Comparison (v5)

Revision 5 of this benchmark created by Digitale Welten on


Preparation HTML

<script>
  /*just a sample function so we have something custom properties can be attached to*/

  cache1 = {};  

  cache2 = [];

  cache3 = function (){};
  cache3.cache = {};

  $cache4 = function(){
    this.cache = {};
  };
  $cache4.prototype.set = function(i, val){ this.cache[i] = val; };
  $cache4.prototype.get = function(i){ return this.cache[i]; };
  $cache4.prototype.del = function(){ this.cache = {}; };
  cache4 = new $cache4();

  cache5 = (function(){

      var cache = {};

      return {
          set: function(i, val){ cache[i] = val; },
          get: function(i){ return cache[i]; },
          del: function(){ cache={}; },
          cache: cache
      }
  }());

  cache6 = new function(){

      this.cache = {};

      return {
          set: function(i, val){ this.cache[i] = val; },
          get: function(i){ return this.cache[i]; },
          del: function(){ this.cache={}; },
          cache: this.cache
      }
  }();

 cache7 = new function(){

      var $this = this;

      $this.cache = {};

      return {
          set: function(i, val){ $this.cache[i] = val; },
          get: function(i){ return $this.cache[i]; },
          del: function(){ $this.cache={}; },
          cache: $this.cache
      }
  }();

 cache8 = (function(){

      var cache = {};

      return {
          set: function(i, val){ this.cache[i] = val; },
          get: function(i){ return this.cache[i]; },
          del: function(){ this.cache={}; },
          cache: this.cache
      }
  }());

  $cache9 = function $cache9(){};
  $cache9.prototype.cache = {};
  $cache9.prototype.set = function(i, val){ this.cache[i] = val; };
  $cache9.prototype.get = function(i){ return this.cache[i]; };
  $cache9.prototype.del = function(){ this.cache={}; };
  cache9 = new $cache9();

  cache10 = new function(){
    this.set = function(key, val){ this[key] = val; };
    this.get = function(key){ return this[key]; };
    this.del = function(){ };
    return this;
  }();

  cache11 = function(){};
  cache11.set = function(key, val){ this[key] = val; };
  cache11.get = function(key){ return this[key]; };
  cache11.del = function(){ };

  cache12 = new function(cache){
    this.cache = cache;
    this.set = function(i, val){ this.cache[i] = val; };
    this.get = function(i){ return this.cache[i]; };
    this.del = function(){ this.cache={}; };
    return this;
  }({});

  cache13 = new function(){
    this.cache = {};
    this.set = function(i, val){ this.cache[i] = val; };
    this.get = function(i){ return this.cache[i]; };
    this.del = function(){ this.cache={}; };
    return this;
  };

  cache14 = new function(){
    this.set = function(i, val){ cache1[i] = val; };
    this.get = function(i){ return cache1[i]; };
    this.del = function(){ cache1 = {}; };
    return this;
  };

  setter = function(i, val){ cache1[i] = val; };
  getter = function(i){ return cache1[i]; };
  delete1 = function(){ cache1={}; };

  setter2 = function(cache, i, val){ cache[i] = val; }; // <-- arr lookup
  getter2 = function(cache, i){ return cache[i]; }; // <-- arr lookup
  delete2 = function(){ cache1={}; };

  setter3 = function(item, val){ item = val; }; // <-- inline cache
  getter3 = function(item){ return item; }; // <-- inline cache
  //delete3 = function(){ };

  setter4 = function(item, val){ if(item!==val) item = val; }; // <-- lazy pattern
  getter4 = function(item){ return item; };
  //delete4 = function(){ };

</script>

Setup

cache1 = {};
    cache2 = [];
    cache3.cache = {};
    cache4.del();
    cache5.del();
    cache6.del();
    cache7.del();
    cache8.del();
    cache9.del();
    //cache10.cache = {};
    //cache11.cache = {};
    cache12.del();
    cache13.del();
    cache14.del();
    delete1();
    delete2();

Test runner

Ready to run.

Testing in
TestOps/sec
Object Array
cache1.num_foo = 345745;
cache1.num_bar = 76.453;
cache1.string_foo = 'string_foo';
cache1.string_bar = 'string_bar';
cache1.func_foo = function() {};
cache1.func_bar = function() {};
cache1.object_foo = {};
cache1.object_bar = {};
cache1.array_foo = [];
cache1.array_bar = [];
ready
Simple Array
cache2.num_foo = 345745;
cache2.num_bar = 76.453;
cache2.string_foo = 'string_foo';
cache2.string_bar = 'string_bar';
cache2.func_foo = function() {};
cache2.func_bar = function() {};
cache2.object_foo = {};
cache2.object_bar = {};
cache2.array_foo = [];
cache2.array_bar = [];
ready
Function Properties
cache3.num_foo = 345745;
cache3.num_bar = 76.453;
cache3.string_foo = 'string_foo';
cache3.string_bar = 'string_bar';
cache3.func_foo = function() {};
cache3.func_bar = function() {};
cache3.object_foo = {};
cache3.object_bar = {};
cache3.array_foo = [];
cache3.array_bar = [];
ready
Prototype
cache4.set('num_foo', 345745);
cache4.set('num_bar', 76.453);
cache4.set('string_foo', 'string_foo');
cache4.set('string_bar', 'string_bar');
cache4.set('func_foo', function() {});
cache4.set('func_bar', function() {});
cache4.set('object_foo', {});
cache4.set('object_bar', {});
cache4.set('func_foo', []);
cache4.set('func_bar', []);
ready
Module Pattern 1
cache5.set('num_foo', 345745);
cache5.set('num_bar', 76.453);
cache5.set('string_foo', 'string_foo');
cache5.set('string_bar', 'string_bar');
cache5.set('func_foo', function() {});
cache5.set('func_bar', function() {});
cache5.set('object_foo', {});
cache5.set('object_bar', {});
cache5.set('func_foo', []);
cache5.set('func_bar', []);
ready
Module Pattern 2
cache6.set('num_foo', 345745);
cache6.set('num_bar', 76.453);
cache6.set('string_foo', 'string_foo');
cache6.set('string_bar', 'string_bar');
cache6.set('func_foo', function() {});
cache6.set('func_bar', function() {});
cache6.set('object_foo', {});
cache6.set('object_bar', {});
cache6.set('func_foo', []);
cache6.set('func_bar', []);
ready
Module Pattern 3
cache7.set('num_foo', 345745);
cache7.set('num_bar', 76.453);
cache7.set('string_foo', 'string_foo');
cache7.set('string_bar', 'string_bar');
cache7.set('func_foo', function() {});
cache7.set('func_bar', function() {});
cache7.set('object_foo', {});
cache7.set('object_bar', {});
cache7.set('func_foo', []);
cache7.set('func_bar', []);
ready
Module Pattern 4
cache8.set('num_foo', 345745);
cache8.set('num_bar', 76.453);
cache8.set('string_foo', 'string_foo');
cache8.set('string_bar', 'string_bar');
cache8.set('func_foo', function() {});
cache8.set('func_bar', function() {});
cache8.set('object_foo', {});
cache8.set('object_bar', {});
cache8.set('func_foo', []);
cache8.set('func_bar', []);
ready
Prototype
cache9.set('num_foo', 345745);
cache9.set('num_bar', 76.453);
cache9.set('string_foo', 'string_foo');
cache9.set('string_bar', 'string_bar');
cache9.set('func_foo', function() {});
cache9.set('func_bar', function() {});
cache9.set('object_foo', {});
cache9.set('object_bar', {});
cache9.set('func_foo', []);
cache9.set('func_bar', []);
ready
Function Properties 2
cache10.set('num_foo', 345745);
cache10.set('num_bar', 76.453);
cache10.set('string_foo', 'string_foo');
cache10.set('string_bar', 'string_bar');
cache10.set('func_foo', function() {});
cache10.set('func_bar', function() {});
cache10.set('object_foo', {});
cache10.set('object_bar', {});
cache10.set('func_foo', []);
cache10.set('func_bar', []);
ready
Function Properties 3
cache11.set('num_foo', 345745);
cache11.set('num_bar', 76.453);
cache11.set('string_foo', 'string_foo');
cache11.set('string_bar', 'string_bar');
cache11.set('func_foo', function() {});
cache11.set('func_bar', function() {});
cache11.set('object_foo', {});
cache11.set('object_bar', {});
cache11.set('func_foo', []);
cache11.set('func_bar', []);
ready
Function Properties 4
cache12.set('num_foo', 345745);
cache12.set('num_bar', 76.453);
cache12.set('string_foo', 'string_foo');
cache12.set('string_bar', 'string_bar');
cache12.set('func_foo', function() {});
cache12.set('func_bar', function() {});
cache12.set('object_foo', {});
cache12.set('object_bar', {});
cache12.set('func_foo', []);
cache12.set('func_bar', []);
ready
Function Properties > Object Array
cache13.set('num_foo', 345745);
cache13.set('num_bar', 76.453);
cache13.set('string_foo', 'string_foo');
cache13.set('string_bar', 'string_bar');
cache13.set('func_foo', function() {});
cache13.set('func_bar', function() {});
cache13.set('object_foo', {});
cache13.set('object_bar', {});
cache13.set('func_foo', []);
cache13.set('func_bar', []);
ready
Helper Functions (Values Passed)
setter('num_foo', 345745);
setter('num_bar', 76.453);
setter('string_foo', 'string_foo');
setter('string_bar', 'string_bar');
setter('func_foo', function() {});
setter('func_bar', function() {});
setter('object_foo', {});
setter('object_bar', {});
setter('func_foo', []);
setter('func_bar', []);
ready
Helper Functions (Array Passed)
setter2(cache1, 'num_foo', 345745);
setter2(cache1, 'num_bar', 76.453);
setter2(cache1, 'string_foo', 'string_foo');
setter2(cache1, 'string_bar', 'string_bar');
setter2(cache1, 'func_foo', function() {});
setter2(cache1, 'func_bar', function() {});
setter2(cache1, 'object_foo', {});
setter2(cache1, 'object_bar', {});
setter2(cache1, 'func_foo', []);
setter2(cache1, 'func_bar', []);
ready
Helper Functions (Property Passed +Inline Cache)
setter3(cache1.num_foo, 345745);
setter3(cache1.num_bar, 76.453);
setter3(cache1.string_foo, 'string_foo');
setter3(cache1.string_bar, 'string_bar');
setter3(cache1.func_foo, function() {});
setter3(cache1.func_bar, function() {});
setter3(cache1.object_foo, {});
setter3(cache1.object_bar, {});
setter3(cache1.func_foo, []);
setter3(cache1.func_bar, []);
ready
Helper Functions (Property Passed +Lazy Pattern)
setter4(cache1.num_foo, 345745);
setter4(cache1.num_bar, 76.453);
setter4(cache1.string_foo, 'string_foo');
setter4(cache1.string_bar, 'string_bar');
setter4(cache1.func_foo, function() {});
setter4(cache1.func_bar, function() {});
setter4(cache1.object_foo, {});
setter4(cache1.object_bar, {});
setter4(cache1.func_foo, []);
setter4(cache1.func_bar, []);
ready
Object Array Wrapped Closure
(function(c) {
  c.num_foo = 345745;
  c.num_bar = 76.453;
  c.string_foo = 'string_foo';
  c.string_bar = 'string_bar';
  c.func_foo = function() {};
  c.func_bar = function() {};
  c.object_foo = {};
  c.object_bar = {};
  c.array_foo = [];
  c.array_bar = [];
}(cache1));
ready

Revisions

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

  • Revision 1: published by Addy Osmani on
  • Revision 2: published by Digitale Welten on
  • Revision 5: published by Digitale Welten on
  • Revision 6: published by Digitale Welten on