Coordinates: Array vs. Object vs. Simple Class

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Array
const objects = [];
for (let i=0; i < 1000000; i++) {
	objects.push([i, i+0.5])
}

const sums = [];
for (let i=0; i < 1000000; i++) {
	sums.push(objects[i][0] + objects[i][1]);
}
ready
Object
const objects = [];
for (let i=0; i < 1000000; i++) {
	objects.push({x: i, y: i+0.5})
}

const sums = [];
for (let i=0; i < 1000000; i++) {
	sums.push(objects[i].x + objects[i].y);
}

ready
Simple Class

class Clazz {
	arr = [];
	constructor(x, y) {
		this.arr[0] = x;
		this.arr[1] = y;
	}
	
	get x() {
		return this.arr[0];
	}
	get y() {
		return this.arr[1];
	}
}

const objects = [];
for (let i=0; i < 1000000; i++) {
	objects.push(new Clazz(i, i+0.5))
}

const sums = [];
for (let i=0; i < 1000000; i++) {
	sums.push(objects[i].x + objects[i].y);
}
ready

Revisions

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