Lerp Angle. trig vs ifmod

Benchmark created on


Setup

const PI   = 3.1415926535897932384626433832795;
const PI2  = 6.2831853071795864769252867665590;
const PI_2 = 1.5707963267948966192313216916398;

Test runner

Ready to run.

Testing in
TestOps/sec
trigonometry
angLerp=( ang1, ang2, mf )=>{
	let cos = (1 - mf) * Math.cos(ang1) + mf * Math.cos(ang2);
	let sin = (1 - mf) * Math.sin(ang1) + mf * Math.sin(ang2);
	return (Math.atan2( sin, cos )+PI2) % PI2;
}

let v = 0;
for( let i = 0; i < 100000; i++ )
	v += angLerp( i % 123 - 45, i % 67.89, 0.5 );

ready
if, mod, wrap
angLerp=( a,b, x )=>{
	let moda = (a % PI2 + PI2) % PI2;
	let modb = (b % PI2 + PI2) % PI2;
	if( Math.abs( moda - modb ) > PI ){
		if( moda < modb ){
			moda += PI2;
		}else{
			modb += PI2;
		}
	}
	return (moda*(1 - x) + modb*x) % PI2;
}

let v = 0;
for( let i = 0; i < 100000; i++ )
	v += angLerp( i % 123 - 45, i % 67.89, 0.5 );
ready

Revisions

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