Math.hypot

Benchmark created by Mythmon on


Description

Compares the speed of Math.hypot vs Math.sqrt for distance.

Setup

var i, data = [], testSize = 1000;
    
    for (i=0; i<testSize; i++) {
        data.push([Math.random(), Math.random()]);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Math.sqrt
var lengths = [];

for (i=0; i<testSize; i++) {
    lengths.push(Math.sqrt(data[i][0]*data[i][0] + data[i][1]*data[i][1]));
}
ready
Math.hypot
var lengths = [];

for (i=0; i<testSize; i++) {
    lengths.push(Math.hypot(data[i][0], data[i][1]));
}
ready
Math.sqrt 2
var lengths = [];

for (i=0; i<testSize; i++) {
    lengths.push(Math.sqrt(Math.pow(data[i][0], 2) + Math.pow(data[i][1], 2)));
}
ready
Math.sqrt 3
var lengths = [];
var x, y;

for (i=0; i<testSize; i++) {
    x = data[i][0];
    y = data[i][1];
    lengths.push(Math.sqrt(x * x + y * y));
}
ready
Math.sqrt 4
var lengths = [];
var x, y;

for (i=0; i<testSize; i++) {
    x = data[i][0];
    y = data[i][1];
    lengths.push(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)));
}
ready

Revisions

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

  • Revision 1: published by Mythmon on