Data locality: Entity updating

JavaScript performance comparison

Test case created by onebytegone

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  class Component {
     constructor() {
        this.value = 0;
     }
     update() {
        this.value = this.value + 1;
     }
  }
  
  class Entity {
     constructor(id) {
        this.id = id;
        this._components = [];
     }
     addComponent(name, component) {
        this._components[name] = component;
     }
     update(name) {
        this._components[name].update();
     }
  }
  
  class ClassComponentSystem {
     constructor() {
        this._components = [];
     }
     add(component) {
        this._components.push(component);
     }
     update() {
        this._components.forEach((component) => {
           component.update();
        });
     }
  }
  
  class PlainObjectComponentSystem {
     constructor() {
        this._components = [];
     }
     add(component) {
        this._components.push(component);
     }
     update() {
        this._components.forEach((component) => {
           component.value = component.value + 1;
        });
     }
  }
  
  const prebuiltEntities = [],
        prebuiltClassSystems = [ new ClassComponentSystem(), new ClassComponentSystem(), new ClassComponentSystem(), new ClassComponentSystem(), new ClassComponentSystem() ];
        prebuiltPlainObjectSystems = [ new PlainObjectComponentSystem(), new PlainObjectComponentSystem(), new PlainObjectComponentSystem(), new PlainObjectComponentSystem(), new PlainObjectComponentSystem() ];
  
  for (let i = 0; i < 10000; i++) {
     const entity = new Entity();
  
     entity.addComponent(0, new Component());
     entity.addComponent(1, new Component());
     entity.addComponent(2, new Component());
     entity.addComponent(3, new Component());
     entity.addComponent(4, new Component());
     prebuiltEntities.push(entity);
  
     prebuiltClassSystems[0].add(new Component());
     prebuiltClassSystems[1].add(new Component());
     prebuiltClassSystems[2].add(new Component());
     prebuiltClassSystems[3].add(new Component());
     prebuiltClassSystems[4].add(new Component());
  
     prebuiltPlainObjectSystems[0].add({ value: 0 });
     prebuiltPlainObjectSystems[1].add({ value: 0 });
     prebuiltPlainObjectSystems[2].add({ value: 0 });
     prebuiltPlainObjectSystems[3].add({ value: 0 });
     prebuiltPlainObjectSystems[4].add({ value: 0 });
  }

};
</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
Array of entities
[ 0, 1, 2, 3, 4 ].forEach((name) => {
   prebuiltEntities.forEach((entity) => {
      entity.update(name);
   });
});
pending…
Class-based component system
prebuiltClassSystems.forEach((system) => {
   system.update();
});
pending…
Plain object component system
prebuiltPlainObjectSystems.forEach((system) => {
   system.update();
});
pending…

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

Compare results of other browsers

0 Comments