Math.sqrt() vs. Newton Raphson Method

JavaScript performance comparison

Test case created by Rob Ede

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Math.sqrt
for (var n = 1; n < 100; n++) {
  Math.sqrt(n);
}
pending…
root
for (var n = 1; n < 100; n++) {

  if (n == 0) return 0;
  if (n < 0) return NaN;
  var itts = 1000;
  var xb2, xb1;
  var x0 = 1;
  x0 = n < 1 ? n * 100 : n / 100;
  var i;
  for (i = 0; i < itts; i++) {
    xb2 = xb1;
    xb1 = x0;
    var xn = x0 - ((x0 * x0 - n) / (2 * x0));
    if (xn == xb1) {
      break;
    }
    if (xn == xb2) {
      x0 = (xb1 + xb2) / 2;
      break;
    }
    x0 = xn;
  }

}
pending…

Compare results of other browsers

Revisions

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

0 Comments