CacheLocality (v2)

Revision 2 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 arr= [];
const preInitArr=arr[SIZE];
const mapInt = new Map();
const mapStr = new Map();
var sum = 0;
for (let i = 0; i < SIZE; i++) {
	/*Use random IDs to avoid backing up by arays*/
	const id = i + Date.now();
	const obj = {id: id, a:getRandomInt(SIZE), b:getRandomInt(SIZE), r:0, idCopy: 0};
	arr.push(obj);
	mapInt.set(id, obj);
	mapStr.set('a' + id, obj);
}

console.log("Map size", mapInt.size);

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

Test runner

Ready to run.

Testing in
TestOps/sec
Array foreach
arr.forEach((o) => {
	o.idCopy = o.id;
	o.r = o.a+o.b;
	sum += o.r;
});
ready
Map with integer key: test entries performance
for(const [id, o] of mapInt.entries()) {
	o.idCopy = id;
	o.r = o.a+o.b;
	sum += o.r;
}
ready
Array for: const in loop
for(let i=0; i<arr.length; i++) {
	const o = arr[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 mapStr.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 mapInt.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 mapStr.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<arr.length; i++) {
	o = arr[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.