buffer vs obj

Benchmark created on


Setup

 
class GameObject {
	constructor () {
	this.position = {
		x: Math.random(),
		y: Math.random(),
		z: Math.random(),
	}
	
	this.velocity = {
		x: Math.random(),
		y: Math.random(),
		z: Math.random(),
	}		
	}
	

	
}

const gameObjects = new Array(1000).fill(0).map(() => new GameObject());


// ------

const positionBuffer = new Array(1000).fill(0).map(() => [Math.random(), Math.random(), Math.random()]).flat();

const velocityBuffer = new Array(1000).fill(0).map(() => [Math.random(), Math.random(), Math.random()]).flat();

const massBuffer = new Array(3000).fill(0).map(() => Math.random())

// -----

class Polluted_GameObject {
	constructor () {
	this.position = {
		x: Math.random(),
		y: Math.random(),
		z: Math.random(),
	}
	
	this.velocity = {
		x: Math.random(),
		y: Math.random(),
		z: Math.random(),
	}		
	
	this.a = Math.random();
	this.b = Math.random();
	this.c = Math.random();
	this.d = Math.random();
	this.e = Math.random();
	this.f = Math.random();
	this.g = Math.random();
	this.h = Math.random();
	this.s1 = "sdfsdfsdfsdfsdfsdf";
	this.s2 = "sdfsdfsdfsdfsdfsdf";
	this.s3 = "sdfsdfsdfsdfsdfsdf";
	this.s4 = "sdfsdfsdfsdfsdfsdf";
	this.s5 = "sdfsdfsdfsdfsdfsdf";
	
	this.arr = new Array(100).fill(0).map(() => Math.random());
	this.arr2 = new Array(100).fill(0).map(() => Math.random());
	this.arr3 = new Array(100).fill(0).map(() => Math.random());
	}
	

	
}

const p_gameObjects = new Array(1000).fill(0).map(() => new Polluted_GameObject());

Test runner

Ready to run.

Testing in
TestOps/sec
obj

const tempDir = {x: 0, y: 0, z: 0};

for (let i = 0; i < 1000; i++) {
	for (let j = 0; j < 1000; j++) {
		tempDir.x = gameObjects[i].position.x - gameObjects[j].position.x;
		tempDir.y = gameObjects[i].position.y - gameObjects[j].position.y;
		tempDir.z = gameObjects[i].position.z - gameObjects[j].position.z;
				
				
		const dist = Math.sqrt(tempDir.x ** 2 + tempDir.y ** 2 + tempDir.z ** 2);
		
		const force = gameObjects[i].mass * gameObjects[i].mass / dist;		
	
	
		tempDir.x *= force;
		tempDir.y *= force;
		tempDir.z *= force;

		gameObjects[i].velocity.x += tempDir.x;
		gameObjects[i].velocity.y += tempDir.y;
		gameObjects[i].velocity.z += tempDir.z;
	}
	

	gameObjects[i].position.x += gameObjects[i].velocity.x;	
	gameObjects[i].position.y += gameObjects[i].velocity.y;	
	gameObjects[i].position.z += gameObjects[i].velocity.z	
}
ready
buffer
const tempDir = {x: 0, y: 0, z: 0};

for (let i = 0; i < 3000; i+=3) {
	for (let j = 0; j < 3000; j+=3) {
		tempDir.x = positionBuffer[i] - positionBuffer[j];
		tempDir.y = positionBuffer[i + 1] - positionBuffer[j + 1];
		tempDir.z = positionBuffer[i + 2] - positionBuffer[j + 2];
		
		const dist = Math.sqrt(tempDir.x ** 2 + tempDir.y ** 2 + tempDir.z ** 2);
		
		const force = massBuffer[i] * massBuffer[j] / dist;		
		
		tempDir.x *= force;
		tempDir.y *= force;
		tempDir.z *= force;
		
		velocityBuffer[i] += tempDir.x;
		velocityBuffer[i+1] += tempDir.y;
		velocityBuffer[i+2] += tempDir.z;
	} 
	
	positionBuffer[i] += velocityBuffer[i];
	positionBuffer[i+1] += velocityBuffer[i+1];
	positionBuffer[i+2] += velocityBuffer[i+2];
	
}
ready
polluted gameobject

const tempDir = {x: 0, y: 0, z: 0};

for (let i = 0; i < 1000; i++) {
	for (let j = 0; j < 1000; j++) {
		tempDir.x = p_gameObjects[i].position.x - gameObjects[j].position.x;
		tempDir.y = p_gameObjects[i].position.y - gameObjects[j].position.y;
		tempDir.z = p_gameObjects[i].position.z - gameObjects[j].position.z;
				
				
		const dist = Math.sqrt(tempDir.x ** 2 + tempDir.y ** 2 + tempDir.z ** 2);
		
		const force = p_gameObjects[i].mass * p_gameObjects[i].mass / dist;		
	
	
		tempDir.x *= force;
		tempDir.y *= force;
		tempDir.z *= force;

		p_gameObjects[i].velocity.x += tempDir.x;
		p_gameObjects[i].velocity.y += tempDir.y;
		p_gameObjects[i].velocity.z += tempDir.z;
	}
	

	p_gameObjects[i].position.x += p_gameObjects[i].velocity.x;	
	p_gameObjects[i].position.y += p_gameObjects[i].velocity.y;	
	p_gameObjects[i].position.z += p_gameObjects[i].velocity.z	
}
ready

Revisions

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