squarerooty

JavaScript performance comparison

Test case created by Simon

Preparation code

<script>

var myRoots = [];
for (var i = 0; i <= 2000; i++) {
  myRoots[i] = Math.sqrt(i);
}


function mySqrt(n) {
  if (n <= 0) return 0;
  if (n < 1) {
    var x = 1 / n;
    if (x <= 2000) {
      return 1 / (myRoots[x | 0]);
    } else {
      return Math.sqrt(n);
    }
  } else {
    if (n <= 2000) {
      return myRoots[n | 0];
    } else {
      return Math.sqrt(n);
    }
  }
}

var a = 0;

</script>
<script>
Benchmark.prototype.teardown = function() {
    a = 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
Math with 0..1
for (var i = 0; i < 1; i+= 0.005){
a +=  Math.sqrt(i);
}
pending…
mySqrt with 0..1
for (var i = 0; i < 1; i+= 0.005){
a +=  mySqrt(i);
}
pending…
Math with integers
for (var i = 0; i < 3000; i++){
a +=  Math.sqrt(i);
}
pending…
mySqrt with integers
for (var i = 0; i < 3000; i++){
a += mySqrt(i);
}
pending…
static math
a += Math.sqrt(1000);
pending…
static my
a += mySqrt(1000);
pending…

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

Compare results of other browsers

0 comments

Add a comment