Sqrt vs Math.sqrt

JavaScript performance comparison

Revision 3 of this test case created

Info

https://gist.github.com/1239433

Preparation code

<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);
    };
  })();
</script>
<script>
Benchmark.prototype.setup = function() {
    number = 2;
    number2 = 3;
   
    function setup() {
     number = Math.random() * 10000 | 0;
     number2 = Math.random() * 10000 | 0;
    }
};
</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
sqrt(number);
sqrt(number2);
pending…
Math.sqrt
Math.sqrt(number);
Math.sqrt(number2);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment