# What is faster than Math.sqrt

## JavaScript performance comparison

Revision 4 of this test case created by Jesper Petersson

## Info

What javascript code is faster than Math.sqrt(x)? answer, not much

## Preparation code

<script>
var arr = [];
for (var i = 0; i < 100; i++) {
arr[i] = i * i;
}

function test2(x) {
return x > 0;
}

function test3(x) {
return x > 0 ? true : false;
}

function test4(x) {
if (x > 0) {
return true;
} else {
return false;
}
}

function test5(x) {
return x * x;
}

function test6(x) {
return arr[x];
}

function test7(x) {
var i = 10;
while (i--);
}

function test8(x) {
Math.pow(x, 0.5);
}
</script>

## Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
sqrt
var result;

for (var i = 0; i < 100; i++) {
result = Math.sqrt(i);
}
pending…
condition
var result;

for (var i = 0; i < 100; i++) {
result = test2(i);
}
pending…
ternary
var result;

for (var i = 0; i < 100; i++) {
result = test3(i);
}
pending…
if
var result;

for (var i = 0; i < 100; i++) {
result = test4(i);
}
pending…
square
var result;

for (var i = 0; i < 100; i++) {
result = test5(i);
}
pending…
array access
var result;

for (var i = 0; i < 100; i++) {
result = test6(i);
}
pending…
loop
var result;

for (var i = 0; i < 100; i++) {
result = test7(i);
}
pending…
^0.5
var result;

for (var i = 0; i < 100; i++) {
result = test8(i);
}
pending…

## Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page: