CacheLocality (v6)

Revision 6 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(a, b) {
        this.a = a;
        this.b = b;
        this.r = 0;
        this.idCopy = 0;
    }
    operate() {
    	this.r = this.a + this.b;
    	return this.r;
    }
    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;
	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;
	sum += o.r;
}
ready
Map with string key: test entries performance
for(const [id, o] of mapWithStrKey.entries()) {
	o.idCopy = id;
	o.r = o.a+o.b;
	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;
	sum += o.r;
}
ready
Map with string key: test values performance
for(const o of mapWithStrKey.values()) {
	o.idCopy = o.id;
	o.r = o.a+o.b;
	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;
	sum += o.r;
}
ready

Revisions

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