Hypot vs Sqrt (v4)

Revision 4 of this benchmark created on


Setup

let total = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
sqrt
total = 0;
for(let i = 0; i < 1000; i++) {
	let x = Math.random();
	let y = Math.random();
	total += Math.sqrt(x * x + y * y);
}
ready
hypot
total = 0;
for(let i = 0; i < 1000; i++) {
	let x = Math.random();
	let y = Math.random();
	total += Math.hypot(x, y);
}
ready
sqrt (pow operator)
total = 0;
for(let i = 0; i < 1000; i++) {
	let x = Math.random();
	let y = Math.random();
	total += (x ** 2 + y ** 2) ** 0.5;
}
ready
Gross, why are you using sqrt anyways?
total = 0;
for(let i = 0; i < 1000; i++) {
	let x = Math.random();
	let y = Math.random();
	total += (x * x + y * y);
}
ready
Establishing the cost of a random call
total = 0;
for(let i = 0; i < 1000; i++) {
	let x = Math.random();
	let y = 1 - x;
	total += (x * x + y * y);
}
ready
Establishing the cost of a hypot call
total = 0;
for(let i = 0; i < 1000; i++) {
	let x = Math.random();
	let y = 1 - x;
	total += Math.hypot(x, y);
}
ready

Revisions

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