clamp. if, ?, minmax

Benchmark created on


Setup

arr=[];
for(let i = 0; i < 100000; i++)
	arr[i] = Math.random();

Test runner

Ready to run.

Testing in
TestOps/sec
if
clamp = (x, a,b)=>{
	if( x < a ) return a;
	if( x > b ) return b;
	return x;
}

let v=0;
for(let i = 0; i < 100000; i++)
	v+= clamp( arr[i], 0.35, 0.65 );

ready
minmax
clamp = (x, a,b)=>{
	return Math.min( Math.max( x,a ), b);
}

let v=0;
for(let i = 0; i < 100000; i++)
	v+= clamp( arr[i], 0.35, 0.65 );
ready
?
clamp = (x, a,b)=>{
	return x<a?a:x>b?b:x;
}

let v=0;
for(let i = 0; i < 100000; i++)
	v+= clamp( arr[i], 0.35, 0.65 );
ready

Revisions

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