Setter Comparison (v2)

Revision 2 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*/

  var cache1 = {};  

  var cache2 = [];

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

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

  var cache4 = new $cache4();

  var cache5 = (function(){

      var cache = {};

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

  var cache6 = new function(){

      this.cache = {};

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

 var cache7 = new function(){

      var $this = this;

      $this.cache = {};

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

 var cache8 = (function(){

      var cache = {};

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

  var $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]; };
  var cache9 = new $cache9();

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

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

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

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

</script>

Setup

cache1 = {};
    cache2 = [];
    cache3.cache = {};
    cache4.cache = {};
    cache5.cache = {};
    cache6.cache = {};
    cache7.cache = {};
    cache8.cache = {};
    cache9.cache = {};
    //cache10.cache = {};
    //cache11.cache = {};
    cache12.cache = {};

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

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