matrix vs object (v2)

Revision 2 of this benchmark created on


Setup

var keyF = function(y,x) {
	return y*1000+x;
}

var mWidth = 500;

var map = new Map();
var bitMap = [];
var matrix = [];
var linearMatrix = [];
var obj = {};
for(let y = 0; y < 500;y++) {
   matrix[y] = []
   let bits = 0;
   for(let x = 0; x < mWidth; x++) {
      let val = { val: x%2 == 0 };
      matrix[y][x] = val 
      linearMatrix[y*mWidth+x] = val;
      obj[keyF(y,x)] = val;
      map.set(keyF(y,x), val);
      
      if(val.val){
      	bits |= (1 << (mWidth - x - 1))
      }
   }
   
   bitMap[y] = bits;
}

Test runner

Ready to run.

Testing in
TestOps/sec
matrix access
for(let y = 0; y < 100; y++) {
	for(let x = 0; x < 100; x++) {
	    if(matrix[y*2][x*2] != undefined) {
	    	let v = matrix[y][x].val;
	    }
    }
}
ready
object access
for(let y = 0; y < 100; y++) {
	for(let x = 0; x < 100; x++) {
		let o = obj[keyF(y,x)];
	    if(o != undefined) {
	    	let v = o.val;
	    }
    }
}
ready
matrix access opt
for(let y = 0; y < 100; y++) {
	for(let x = 0; x < 100; x++) {
		let m = matrix[y*2][x*2];
	    if(m != undefined) {
	    	let v = m.val;
	    }
    }
}
ready
linear matrix access
for(let y = 0; y < 100; y++) {
	for(let x = 0; x < 100; x++) {
		let m = linearMatrix[y*2*mWidth+x*2];
	    if(m != undefined) {
	    	let v = m.val;
	    }
    }
}
ready
bitmap access
for(let y = 0; y < 100; y++) {
	for(let x = 0; x < 100; x++) {
		let isFilled = (bitMap[y*2] & (1 << (mWidth - (x*2) - 1))) !== 0;
    }
}
ready
map access
for(let y = 0; y < 100; y++) {
	for(let x = 0; x < 100; x++) {
		let o = map.get(keyF(y*2,x*2));
	    if(o != undefined) {
	    	let v = o.val;
	    }
    }
}
ready

Revisions

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