CacheLocality (v8)

Revision 8 of this benchmark created on


Description

Tests to benchamark data locality between arrays, map entries, map keys, map values, object entries, object values. This is used to determine best approach for iterations.

Setup

const SIZE = 10_000;
const objectsArray= [];
const instancesArray= [];
const mapWithIntKey = new Map();
const mapWithStrKey = new Map();
var sum = 0;

class Point {
		constructor(id, x, y) {
			this.id = id;
			this.x = x;
			this.y = y;
			this.med = 0;
			this.idCopy = 0;
		}
		operate() {
			this.med = (this.x + this.y) / 2;
			return this.med;
		}
		copyId(newId) {
			this.idCopy = newId;
		}
	}

for (let i = 0; i < SIZE; i++) {
	/*Use random IDs to avoid backing up by arays*/
	const id = i + Date.now();
	const a = getRandomInt(SIZE);
	const b = getRandomInt(SIZE);
	const obj = {id: id, a:a, b:b, r:0, idCopy: 0};
	objectsArray.push(obj);
	mapWithIntKey.set(id, obj);
	mapWithStrKey.set('a' + id, obj);
	const point = new Point(a, b);
	instancesArray.push(point);
}

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Test class instances
for(let i=0; i<instancesArray.length; i++) {
	const o = instancesArray[i];
	o.copyId(o.id);
	sum += o.operate();
}
ready
Map with integer key: test entries performance
for(const [id, o] of mapWithIntKey.entries()) {
	o.idCopy = id;
	o.r = (o.a+o.b) / 2;
	sum += o.r;
}
ready
Array for: const in loop
for(let i=0; i<objectsArray.length; i++) {
	const o = objectsArray[i];
	o.idCopy = o.id;
	o.r = (o.a+o.b) / 2;
	sum += o.r;
}
ready
Map with integer key: test values performance
for(const o of mapWithIntKey.values()) {
	o.idCopy = o.id;
	o.r = (o.a+o.b) / 2;
	sum += o.r;
}
ready
Array for: var outside loop
var o;
for(let i=0; i<objectsArray.length; i++) {
	o = objectsArray[i];
	o.idCopy = o.id;
	o.r = (o.a+o.b) / 2;
	sum += o.r;
}
ready

Revisions

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