Game objects

Benchmark created on


Setup

let len = 10;

let player = {
	x: 0,
	y: 0,
	w: 16,
	h: 16,
	update() {
		return this.x;
	}
};



function PlayerFactory(x, y) {
	return {
		x: x,
		y: y,
		w: 16,
		h: 16,
		update() {
			return this.x;
		}
	};
}

function Entity(config) {
	return {
		x: x,
		y: y,
		w: 16,
		h: 16,
		update() {
			return this.x;
		},
		...config
	};
}

function PlayerFactoryEntity(config) {
	return Entity(config);
}

function PlayerProto(x, y) {
	this.x = x;
	this.y = y;
	this.w = 16;
	this.h = 16;
	this.update = () => {
		return this.x;
	};
	return this;
}


class EntityClass {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  
  update() {
  	return this.x;
  }
}

class PlayerClass extends EntityClass {
  constructor(x, y) {
    super(x, y);
    this.isPlayer = true;
  }
}

Test runner

Ready to run.

Testing in
TestOps/sec
POJO
let res1 = [];
for (let x=0; x < len; x++) {
	res1.push({...player}.update());
}
//console.log('POJO', res1);
ready
Function factory
let res2 = [];
for (let x=0; x < len; x++) {
	res2.push(PlayerFactory(x, 0).update());
}
//console.log('PlayerFactory', res2)
ready
Function proto
let res3 = [];
for (let x=0; x < len; x++) {
	res3.push(PlayerProto(x, 0).update());
}
//console.log('PlayerProto', res3);
ready
Player Factory Entity
let res4 = [];
for (let x=0; x < len; x++) {
	res4.push(PlayerFactoryEntity({x, y: 0}).update());
}
ready
Player Class
let res5 = [];
for (let x=0; x < len; x++) {
	res5.push(new PlayerClass({x, y: 0}).update());
}
ready

Revisions

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