Data locality: Entity creation

Benchmark created by onebytegone on


Setup

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();

Test runner

Ready to run.

Testing in
TestOps/sec
Array of entities
const entity = new Entity();

entity.addComponent(new Component());
entities.push(entity);
ready
Class-based component system
classSystem.add(new Component());
ready
Plain object component system
plainObjectSystem.add({ value: 0 });
ready

Revisions

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

  • Revision 1: published by onebytegone on