Structure to represent Vectors

Benchmark created on


Description

TypedArray, Float32Array, Object, Class access time read time write time

Preparation HTML

<span></span>

Setup

let span = document.body.querySelector("span");

let oVectors = [];
let vVectors = [];
let fVectors = [];
let cVectors = [];

function Vector (x, y) { 
  this.x = x;
  this.y = y;
}

class Vector32 extends Float32Array {
	
	constructor (x, y) {
		
		super (2);
		
		this[0] = x;
		this[1] = y;
	}
	
	get x () { return this[0]; }
	get y () { return this[1]; }
}

COUNT = 100000;

for (let i = 0; i < COUNT; i++ ) {
	const x = 10000 * (Math.random() - 0.5);
	const y = 10000 * (Math.random() - 0.5);	
	
	const f = new Float32Array(2);
	f[0] = x;
	f[1] = y;
	
	oVectors.push({ x , y });
	vVectors.push(new Vector(x, y));
	fVectors.push(f);
	cVectors.push(new Vector32(x, y));
}

let acc = 0;

Teardown

span.innerText = acc;

Test runner

Ready to run.

Testing in
TestOps/sec
Vector GET
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	acc += vVectors[i].x;
	acc += vVectors[i].y;
}
ready
Vector SET
for (let i = 0; i < COUNT; i++ ) {
	const x = 10000 * (Math.random() - 0.5);
	const y = 10000 * (Math.random() - 0.5);	
	vVectors[i].x = x;
	vVectors[i].y = y;	
}
ready
Float32Array GET
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	acc += fVectors[i][0];
	acc += fVectors[i][1];
}
ready
Float32Array SET
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	const x = 10000 * (Math.random() - 0.5);
	const y = 10000 * (Math.random() - 0.5);	
	fVectors[i][0] = x;
	fVectors[i][1] = y;
}
ready
Vector32 GET ELEMENT
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	acc += cVectors[i][0];
	acc += cVectors[i][1];
}
ready
Vector32 SET ELEMENT
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	const x = 10000 * (Math.random() - 0.5);
	const y = 10000 * (Math.random() - 0.5);	
	cVectors[i][0] = x;
	cVectors[i][1] = y;
}
ready
Vector32 GET GETTER
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	acc += cVectors[i].x;
	acc += cVectors[i].y;
}
ready
Vector32 SET GETTER
for (let i = 0; i < COUNT; i++ ) {
	const x = 10000 * (Math.random() - 0.5);
	const y = 10000 * (Math.random() - 0.5);	
	oVectors[i].x = x;
	oVectors[i].y = y;	
}
ready
POJO GET
acc = 0;

for (let i = 0; i < COUNT; i++ ) {
	acc += oVectors[i].x;
	acc += oVectors[i].y;
}
ready
POJO SET
for (let i = 0; i < COUNT; i++ ) {
	const x = 10000 * (Math.random() - 0.5);
	const y = 10000 * (Math.random() - 0.5);	
	vVectors[i].x = x;
	vVectors[i].y = y;	
}
ready

Revisions

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