Data locality: Entity creation

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(component) {
        this._components.push(component);
     }
     update() {
        this._components.forEach((component) => {
           component.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 entities = [],
        classSystem = new ClassComponentSystem(),
        plainObjectSystem = new PlainObjectComponentSystem();

};
</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
const entity = new Entity();

entity.addComponent(new Component());
entities.push(entity);
pending…
Class-based component system
classSystem.add(new Component());
pending…
Plain object component system
plainObjectSystem.add({ value: 0 });
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