Distance Performance (v10)

Revision 10 of this benchmark created on


Setup


var TOTAL3 = 0;function distance1(_a, _b, in_check) {
	const a_x = _a.x
	const a_y = _a.y
	const b_x = _b.x
	const b_y = _b.y
  
	const a_w2 = _a.width / 2
	const a_h = _a.height
	const b_w2 = _b.width / 2
	const b_h = _b.height

	// TODO: If one is just a single point, we're computing 8 needless calculations.
	// Compare the 4 corners to each other
	let min = 99999999
	for(const a_c of [{ x: a_x + a_w2, y: a_y - a_h},
					  { x: a_x + a_w2, y: a_y},
					  { x: a_x - a_w2, y: a_y - a_h},
					  { x: a_x - a_w2, y: a_y}]) {
	  for(const b_c of [{ x: b_x + b_w2, y: b_y - b_h},
						{ x: b_x + b_w2, y: b_y},
						{ x: b_x - b_w2, y: b_y - b_h},
						{ x: b_x - b_w2, y: b_y}]) {
		const d = Math.hypot(a_c.x - b_c.x, a_c.y - b_c.y)
		if(d < min) min = d
	  }
	}
	return min
}

function distance2(_a, _b, in_check) {

	const a_x = _a.x
	const a_y = _a.y
	const b_x = _b.x
	const b_y = _b.y
  
	const a_w2 = _a.width / 2
	const a_h = _a.height
	const b_w2 = _b.width / 2
	const b_h = _b.height

	let min = 99999999;
	let a_x1 = a_x + a_w2;
	let a_x2 = a_x - a_w2;
	let b_x1 = b_x + b_w2;
	let b_x2 = b_x - b_w2;
	let a_y1 = a_y;
	let a_y2 = a_y - a_h;
	let b_y1 = b_y;
	let b_y2 = b_y - b_h;
    let minX = Math.min(
    	Math.abs(a_x1 - b_x1), 
    	Math.abs(a_x1 - b_x2),
    	Math.abs(a_x2 - b_x1), 
    	Math.abs(a_x2 - b_x2)
    );
    let minY = Math.min(
    	Math.abs(a_y1 - b_y1),
    	Math.abs(a_y1 - b_y2),
    	Math.abs(a_y2 - b_y1), 
    	Math.abs(a_y2 - b_y2)
    );
    return Math.hypot(minX, minY);
}
function distance3(_a, _b, in_check) {
	const a_x = _a.x
	const a_y = _a.y
	const b_x = _b.x
	const b_y = _b.y
  
	const a_w2 = _a.width / 2
	const a_h = _a.height
	const b_w2 = _b.width / 2
	const b_h = _b.height

	let min = 99999999;
	let a_x1 = a_x + a_w2;
	let a_x2 = a_x - a_w2;
	let b_x1 = b_x + b_w2;
	let b_x2 = b_x - b_w2;
	let a_y1 = a_y;
	let a_y2 = a_y - a_h;
	let b_y1 = b_y;
	let b_y2 = b_y - b_h;
    let minX = Math.min(
    	Math.abs(a_x1 - b_x1), 
    	Math.abs(a_x1 - b_x2),
    	Math.abs(a_x2 - b_x1), 
    	Math.abs(a_x2 - b_x2)
    );
    let minY = Math.min(
    	Math.abs(a_y1 - b_y1),
    	Math.abs(a_y1 - b_y2),
    	Math.abs(a_y2 - b_y1), 
    	Math.abs(a_y2 - b_y2)
    );
    return Math.sqrt(minX * minX + minY * minY);
}
var CASES = new Array(100).fill(undefined).map(() => {
	return [
		{
			x: Math.random() * 100 - 50,
			y: Math.random() * 100 - 50,
			width: Math.random() * 10,
			height: Math.random() * 10
		}, 
		{
			x: Math.random() * 100 - 50,
			y: Math.random() * 100 - 50,
			width: Math.random() * 10,
			height: Math.random() * 10
		}
	];
});
var TOTAL1 = 0;
var TOTAL2 = 0;
var TOTAL3 = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Old Test
for(let i = 0; i < CASES.length; i++) {
	TOTAL1 += distance1(CASES[i][0], CASES[i][1]);	
}
ready
New Test
for(let i = 0; i < CASES.length; i++) {
	TOTAL2 += distance2(CASES[i][0], CASES[i][1]);	
}
ready
Sanity
for(let i = 0; i < CASES.length; i++) {
	let [a, b] = CASES[i];
	if(distance1(a, b) != distance2(a, b)) {
		console.error(distance1(a, b), distance2(a, b))
		console.error(a, b);
		throw new Error(1);
	}
}
ready
Sqrt instead of Hypot
for(let i = 0; i < CASES.length; i++) {
	TOTAL3 += distance3(CASES[i][0], CASES[i][1]);	
}
ready

Revisions

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