Sqrt vs Math.sqrt (v2)

Revision 2 of this benchmark created by Daniel Davis on


Description

https://gist.github.com/1239433

Preparation HTML

<script>
  var sqrt = (function () {
  "use strict";
  
  function sqrt (n, x, p) {
  return Math.abs(x*x - n) < p ? x : sqrt(n, (n/x + x) / 2, p);
  }
  
  return function (n) {
  return sqrt(n, 1, 0.1);
  };
  })();
  
  // Alternative to Math.abs
  function abs(x) {
      return (x < 0 ? -x : x);
  }
  
  var fast_sqrt = (function () {
  "use strict";
  
  function sqrt (n, x, p) {
  return abs(x*x - n) < p ? x : sqrt(n, (n/x + x) / 2, p);
  }
  
  return function (n) {
  return sqrt(n, 1, 0.1);
  };
  })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Sqrt
sqrt(2);
sqrt(3);
ready
Math.sqrt
Math.sqrt(2);
Math.sqrt(3);
ready
Alternative sqrt
fast_sqrt(2);
fast_sqrt(3);
ready

Revisions

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

  • Revision 1: published by sqrt vs Math.sqrt on
  • Revision 2: published by Daniel Davis on