Object plain vs Class

JavaScript performance comparison

Test case created by manufitoussiwit

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var props = {};
  for (var i = 0; i < 50; i++) {
      props['prop' + i] = 'prop' + i;
  }
  
  var setup = {
      prop0: 'coco',
      prop1: 'tata',
      prop2: 'coucou',
      prop3: 'chat'
  };
  
  var ClassObject = function(key) {
      this.key = key;
      this.value = null;
  };
  
  ClassObject.prototype.defaultValue = function() {
      return props[this.key];
  }
  ;
  
  var createObjectWithClass = ()=>{
      var obj = {};
      Object.keys(props).forEach(key=>{
          obj[key] = new ClassObject(key);
      }
      );
      Object.entries(setup).forEach(([key,value])=>{
          obj[key].value = value;
      }
      );
      return obj;
  }
  ;
  
  var createObjectWithPlain = ()=>{
      var obj = {};
      Object.keys(props).forEach(key=>{
          obj[key] = {
              value: null,
              defaultValue: function() {
                  return props[obj[key].key];
              }
          };
      }
      );
      Object.entries(setup).forEach(([key,value])=>{
          obj[key].value = value;
      }
      );
      return obj;
  }
  
  var ProtoObjectWithAssign = function() {
      var obj = Object.assign(Object.create(props), setup);
      return obj;
  };
  
  var ProtoObjectWithParcourt = function() {
      var obj = Object.create(props);
      Object.keys(setup).forEach((key)=>{
          obj[key] = setup[key];
      }
      );
      return obj;
  };
  
  var defineProp = (p,name,val)=>{
      p['_' + name] = val;
      Object.defineProperty(p, name, {
          get() {
              return this['_' + name];
          },
          set(value) {
              this['_' + name] = value;
          }
      });
  }
  
  var proto = {};
  
  Object.entries(props).forEach(([key,val])=>{
      defineProp(proto, key, val);
  }
  );
  
  var ProtoClassWithAssign = function() {
      return Object.assign(Object.create(proto), setup);
  };
  
  var ProtoClassWithAssign2 = function() {
      var _setup = {};
      Object.keys(setup).forEach(key=>{
          _setup['_' + key] = setup[key];
      }
      );
      return Object.assign(Object.create(proto), _setup);
  };
  
  var ProtoClassWithParcourt = function() {
      var obj = Object.create(proto);
      Object.keys(setup).forEach(key=>{
          obj['_' + key] = setup[key];
      }
      );
      return obj;
  };
  

};

Benchmark.prototype.teardown = function() {
   

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
ProtoObjectWithAssign
ProtoObjectWithAssign();
pending…
ClassObject
createObjectWithClass();
pending…
Plain Object
createObjectWithPlain();
pending…
ProtoObjectWithParcourt
ProtoObjectWithParcourt();
pending…
ProtoClassWithAssign
ProtoClassWithAssign();
pending…
ProtoClassWithAssign2
ProtoClassWithAssign2();
pending…
ProtoClassWithParcourt
ProtoClassWithParcourt()
pending…

Compare results of other browsers

Revisions

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

0 Comments